gpt4 book ai didi

python - if 语句中冒号的语法错误

转载 作者:IT老高 更新时间:2023-10-28 21:18:07 35 4
gpt4 key购买 nike

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

最佳答案

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

当你得到一个奇怪的 SyntaxError 时,检查它之前的括号余额。

关于python - if 语句中冒号的语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12732743/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com