Python if 语句中冒号的语法错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12732743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:44:16  来源:igfitidea点击:

Syntax error on the colon in an if statement

pythonpython-3.x

提问by Chimp

I am new to python, and am making a sort-of game as one of my first projects that guesses a number between 1 and 10, then the user guesses it. They have three guesses, and the program tells the user if they need to go higher or lower on their next guess. The part of the code with the error in isn't crucial, as it only makes a guess not be wasted if the user puts in the same answer twice, allowing them to redo their guess the first time but not allowing a re-take the second. On the code, I have marked where the problem is. Like I said, I am really new to python and this is probably some amateur noobie mistake. Thanks in advance.

我是 python 的新手,正在制作一种游戏,作为我的第一个项目之一,它猜测 1 到 10 之间的数字,然后用户猜测它。他们有三个猜测,程序会告诉用户他们下一次猜测是否需要更高或更低。有错误的代码部分并不重要,因为如果用户两次输入相同的答案,它只会使猜测不会被浪费,允许他们第一次重做他们的猜测但不允许重新进行第二。在代码上,我已经标记了问题所在。就像我说的,我对 python 真的很陌生,这可能是一些业余新手错误。提前致谢。

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'

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()

采纳答案by Oleh Prypin

It's not actually the colon. It's the unclosed bracket on the previous line.

它实际上不是冒号。它是上一行的未闭合括号。

When you get a weird SyntaxError, check for bracket balance before it.

当你得到一个奇怪的SyntaxError,在它之前检查括号平衡。

回答by Chiel ten Brinke

The line above is missing a parenthesis. Change

上面的行缺少括号。改变

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

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

to

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

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