我想做一个猜数游戏,我写了这段代码:
from random import randint as rand
number=rand(-1,1001)
tries=0
def numguess():
guess=int(input("The chosen number is between 0 to 1000.\nEnter your guess : "))
tries=tries+1
numguess()
while True:
if number==guess:
print ("You won. My number was effectively" ,number,". \n It took you ",tries, "to guess the number.")
break
elif number<guess:
print ("The number I chose is lower than your guess")
numguess()
else:
print ("The number I chose is higher than your guess")
numguess()
当我运行它时,它会询问我输入然后引发UnboundLocalError。有什么我做错了吗?我试过搜索,但我不明白。谢谢。
答案 0 :(得分:0)
您的tries
变量在本地处理 - 因为您为其赋值,请使用:
global tries
tries = tries +1
在您的函数中