Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 函数中的变量错误_Python_Qpython3 - Fatal编程技术网

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
在你的职责范围内