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

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-3.x相关问答推荐

根据另一列值对多个数据框列进行分组

正则表达式:匹配字符串中的分隔符(字母和特殊字符)以形成新的子字符串

ImportError:无法从“jinja2”导入名称“escape”

Semaphore信号量 Python 的工作原理

Python 3.10 模式匹配 (PEP 634) - 字符串中的通配符

所有 Python dunder 方法的列表 - 您需要实现哪些方法才能正确代理对象?

finally 总是在 try 块返回之前运行,那么为什么 finally 块中的更新不会影响 try 块返回的变量的值呢?

Youtube_dl:错误:YouTube 说:无法提取视频数据

Python 无法安装包

ModuleNotFoundError 与 pytest

在 ubuntu 20.04 中安装 libpq-dev 时出现问题

具有不均匀间隙的 Python 范围

ImportError:无法从“sklearn.externals”导入名称“joblib”

os.path.join 是否有 Pathlib 替代项?

是什么导致 [*a] 过度分配?

“with”语句是否支持类型提示?

try 注释散列变量时,“ABCMeta”对象不可下标

如何在 Python 3 中 .decode('string-escape') ?

TypeError:无法实例化类型元组;使用 tuple() 代替

如何解决“No module named 'frontend'”错误消息?