Python 函数中的变量错误
我想做一个数字猜谜游戏,我写了以下代码:Python 函数中的变量错误,python,qpython3,Python,Qpython3,我想做一个数字猜谜游戏,我写了以下代码: 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==gu
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()
从随机导入randint作为rand
数字=兰特(-11001)
尝试=0
def numguess():
guess=int(输入(“所选数字在0到1000之间。\n输入您的猜测:”)
尝试=尝试+1
numguess()
尽管如此:
如果数字==猜测:
打印(“你赢了。我的号码是有效的”,号码“.\n它花了你”,尝试“猜号码。”)
打破
elif number您的trys
变量在本地处理-由于您为其赋值,请使用:
global tries
tries = tries +1
在你的职责范围内