2012-10-04 48 views
15

我是python的新手,我正在做一個排序遊戲作爲我的第一個猜測1到10之間的數字的項目之一,然後用戶猜測它。他們有三次猜測,該程序告訴用戶他們是否需要在他們的下一個猜測中上升或下降。代碼中出現錯誤的部分並不重要,因爲如果用戶兩次輸入相同的答案,它只會讓人猜測不會被浪費,從而允許他們在第一次重新猜出自己的猜測,但不允許重新獲取第二。在代碼上,我標記了問題所在。就像我說的,我對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