我是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相关问答推荐

是什么原因导致Pandas=2.1.4和Pandas=1.4.2之间Pandas DataFrame中从Float64到int32的连续列转换方式不同

我不能使用拆分来分隔数据

如何获取自定义文件上传路径的对象ID?

Pandas教程:如何更新行内数值的位置

从列表的元素和python中的多个多索引数据帧执行方程

在 groupby 之后,Pandas 在特定类别中获得最常见和最后的值

如何使用 django rest 框架在 self forienkey 中删除多达 n 种类型的数据?

正则表达式从文本文件中捕获包含制表符/空格和子字符串的部分字符串

BeautifulSoup 和 pd.read_html - 如何将链接保存到最终数据框中的单独列中?

将名字转换成姓氏、首字母和中间字母的格式

Python 3.5 中编码 utf-8 和 utf8 的区别

多个返回函数的列表理解?

如何判断一个字符串是否包含有效的 Python 代码

在数据类中创建类变量的正确方法

使用 pytest.fixture 返回模拟对象的正确方法

cv2 python 没有 imread 成员

如何模拟 open(...).write() 而不会出现没有这样的文件或目录错误?

计算两个文件的行差异的最有效方法是什么?

如何从 Python 3 导入 FileNotFoundError?

Pylint 中的模块PyQt5.QtWidgets错误中没有名称QApplication