2012-10-04 26 views
15

私はPythonの初心者で、1から10までの数字を推測する私の最初のプロジェクトの1つとしてゲームを作っています。彼らは3つの推測を持っており、プログラムは次の推測で上がったり下がったりする必要があるかどうかをユーザに伝えます。エラーのあるコードの部分は重要ではありません。ユーザーが同じ回答を2回入力した場合、憶測を無駄にしないようにするだけで、最初は推測をやり直すことができますが、秒。コード上で、私はどこに問題があるのか​​を記しました。私が言ったように、私はPythonには本当に新しく、これはおそらく素人のnoobieの間違いです。前もって感謝します。if文のコロンに構文エラーがあります

import time # This imports the time module. 
import random # This imports the random module. 

MyNumber = random.randrange(1,10) # This picks a number for the variable 'MyNumber'. 

# Intro text and instructions. 
print('=====================================') 
print('=Welcome to GuessMyNumber!   =') 
print('=         =') 
print('=I will make a random number between=') 
print('=1 and 10, and you must guess it. If=') 
print('=you are wrong, I will tell you if =') 
print('=you need to go higher or lower. Be =') 
print('=careful, as you only have three =') 
print('=guesses!       =') 
print('=====================================') 
print() 

firstGuess = int(input('Ok then, we shall begin! What is your first guess?')) 
print() 
if firstGuess == (MyNumber): 
print('Well done! You win!') 
time.sleep(3) 
exit() 
if firstGuess < MyNumber: 
print('Go Higher!') 
time.sleep(1) 
if firstGuess > MyNumber: 
print('Go Lower!') 
time.sleep(1) 

print() 
secondGuess = int(input('Better luck this time! What is your second guess?')) 
print() 
if secondGuess == firstGuess: 
print('You tried that one last time! Don\'t worry, I won\'t count that one!') 
bungled = (1) 
secondGuess = int(input('What is your second guess?') 
if secondGuess == firstGuess:#This colon is causing the problem. 
    print('You\'ve already tried that one twice!') 
    bungled = (2) 
if secondGuess == MyNumber: 
print('Well done! You win!') 
time.sleep(3) 
exit() 
if secondGuess < MyNumber: 
print('Go Higher!') 
time.sleep(1) 
if secondGuess > MyNumber: 
print('Go Lower!') 
time.sleep(1) 

print() 
thirdGuess = int(input('This is your final chance! What is your third guess?')) 
print() 
if thirdGuess == MyNumber: 
print('Well done! You win!') 
time.sleep(3) 
exit() 
if thirdGuess < MyNumber: 
MyNumber = str(MyNumber) 
print('Sorry! You lost! The number was '+MyNumber) 
time.sleep(1) 
exit() 
if thirdGuess > MyNumber: 
MyNumber = str(MyNumber) 
print('Sorry! You lost! The number was '+MyNumber) 
time.sleep(1) 
exit() 

答えて

18

それは実際には、コロンではありません。これは前の行の閉じられていない括弧です。

奇妙な場合は、SyntaxErrorの前にブラケットバランスを確認してください。

+0

ありがとう、私はいつもブラケットをチェックします。 – Chimp

2

上記の行に括弧がありません。 変更

secondGuess = int(input('What is your second guess?')

から secondGuess = int(input('What is your second guess?'))

+0

ありがとう、私はチェッカーがそれを選んでいない理由をよく分かりません。すべてが今、完璧に動作しています:) – Chimp

関連する問題