我在第10行看到
时出现语法错误Traceback (most recent call last):
File "main.py", line 5, in <module>
user = input("Choose a number")
File "<string>", line 1
python main.py
^
我无法理解。有人可以帮忙吗?
import random
number = random.randint(1, 20)
attempt = 5
def game():
global attempt
attempt -= 1
if attempt > 0:
if int(user) > number:
user = input("Choose a number")
print("Choose a smaller number\n " + (str(attempt) + " tries left"))
game()
elif int(user) < number:
print("Choose a larger number\n "+ (str(attempt) + " tries left"))
game()
elif int(user) == number:
print ("You guessed the number ")
else:
print("CONGRATULATIONS ")
game()
答案 0 :(得分:2)
首先,这是运行时错误,而不是语法错误。
问题实际上在上一行中:您曾尝试使用user
之前的值。您必须先解决此问题,然后才能继续调试程序。在测试之前,您需要获取用户的第一个号码。