Python 错误:UnboundLocalError:局部变量';尝试';分配前参考
我正在尝试用图形为我的班级做一个简单的猜谜游戏,我正在尝试做一个计数器。这就是我的代码出错的地方Python 错误:UnboundLocalError:局部变量';尝试';分配前参考,python,Python,我正在尝试用图形为我的班级做一个简单的猜谜游戏,我正在尝试做一个计数器。这就是我的代码出错的地方 def value(): guess = int(input("Enter your guess: ")) if guess > num: attempts = attempts + 1 turtle.clearscreen() interface() tooHigh() at
def value():
guess = int(input("Enter your guess: "))
if guess > num:
attempts = attempts + 1
turtle.clearscreen()
interface()
tooHigh()
attempt = turtle.Turtle()
attempt.speed(0)
attempt.color("white")
attempt.penup()
attempt.hideturtle()
attempt.goto(-250 , 200)
attempt.write(guess, font=("Courier", 14, "bold"))
value()
elif guess < num:
attempts = attempts + 1
turtle.clearscreen()
interface()
tooLow()
attempt = turtle.Turtle()
attempt.speed(0)
attempt.color("white")
attempt.penup()
attempt.hideturtle()
attempt.goto(-250 , 200)
attempt.write(guess, font=("Courier", 14, "bold"))
value()
elif guess == num:
attempts = attempts + 1
turtle.clearscreen()
interface()
yes()
attempt = turtle.Turtle()
attempt.speed(0)
attempt.color("pink")
attempt.penup()
attempt.hideturtle()
attempt.goto(-250 , 200)
attempt.write(guess, font=("Courier", 14, "bold", "underline"))
print ("Correct!")
else:
print ("ERROR")
def startScreen():
begin = input("Start the game?: ")
if begin == 'yes':
value()
elif begin == 'instructions':
instructions()
startScreen()
elif begin == 'no':
sys.exit()
else:
print ("Unrecognised answer.")
startScreen()
attempts = 0
num = random.randint(1,1000)
interface()
startScreen()
def值():
guess=int(输入(“输入您的猜测:”)
如果猜测>数值:
尝试次数=尝试次数+1
乌龟
接口()
太高了()
尝试=海龟。海龟()
尝试速度(0)
颜色(“白色”)
尝试
尝试。隐藏()
尝试。转到(-250200)
尝试写入(猜测,字体=(“Courier”,14,“bold”))
值()
elif guess
我收到的错误是:
Traceback (most recent call last):
File "D:\Desktop\Python Programs\Game.py", line 154, in <module>
`startScreen()`
File "D:\Desktop\Python Programs\Game.py", line 141, in startScreen
`value()`
File "D:\Desktop\Python Programs\Game.py", line 110, in value
`attempts = attempts + 1`
UnboundLocalError: local variable 'attempts' referenced before assignment
回溯(最近一次呼叫最后一次):
文件“D:\Desktop\Python Programs\Game.py”,第154行,在
`startScreen()`
startScreen中第141行的文件“D:\Desktop\Python Programs\Game.py”
`值()`
文件“D:\Desktop\Python Programs\Game.py”,第110行,数值
`尝试次数=尝试次数+1`
UnboundLocalError:赋值前引用的局部变量“尝试次数”
似乎不可能将尝试
移动到函数中,因为它不断地调用自己,每次都重置尝试
我不确定为什么会发生这种情况,因此非常感谢您的帮助。谢谢 在函数中,当您访问用于赋值的变量时,默认情况下将其视为局部变量。因此,要让Python知道要修改的变量是全局变量,请执行以下操作:
def值():
全球尝试
#代码的其余部分
出现此错误的原因是一种称为变量范围的错误。您已经在函数的值
之外定义了变量尝试次数
,因此函数将不会通过名称尝试次数
识别任何局部变量,除非您在函数的开头明确地添加了语句全局尝试次数
。这将告诉函数您要使用在主程序中定义的变量尝试
。应该是这样的:
def value():
global attempts
#function code
def value(attempts):
#function code
#you can use the variable attempts as needed, since you're passing it directly into the function
#call the function however you want
attempts = 0
value(attempts)
或者,您可以允许函数值
接受一个参数,并将变量尝试
传递到该参数中。看起来是这样的:
def value():
global attempts
#function code
def value(attempts):
#function code
#you can use the variable attempts as needed, since you're passing it directly into the function
#call the function however you want
attempts = 0
value(attempts)
当您在函数中的某个地方分配了“<代码>尝试> /代码>时,Python将默认地认为它是本地的。但是当您第一次使用它时,当尝试计算尝试+1
时,它没有被给定值,因此出现错误。如果要使用全局尝试
,则必须使其明确。在函数的开头添加全局尝试
。是否有任何原因需要跟踪函数之外的尝试
?如果没有,只需将truments=0
移动到函数的开头,初始化函数本身中的局部变量。我想到了这一点,但是函数稍后在代码中调用自身,这意味着如果我在函数中定义truments
,它会不断地重置自身。对。我错过了您在其内部调用value()
的部分。这确实会重置计数器。所以,是的,它应该在外面。我会更新答案的。啊。我假设您必须在全局
下定义变量。更改了代码,它就工作了。非常感谢。