简体   繁体   English

Python UnboundLocalError:分配前引用了局部变量

[英]Python UnboundLocalError: local variable referenced before assignment

I'm confused. 我糊涂了。 What is different about player1_head compared to the other variables I am printing in the code below? 与我在下面的代码中打印的其他变量相比, player1_head有何不同? As far as I can tell it should behave the same as the others - it's declared in the global scope, no? 据我所知,它的行为应该与其他行为相同-是在全局范围内声明的,不是吗? I don't think it's a typo. 我不认为这是错字。

UnboundLocalError: local variable 'player1_head' referenced before assignment UnboundLocalError:分配前已引用局部变量“ player1_head”

from turtle import *
from random import randint
from utils import square, vector

player1_xy = vector(-100, 0)
player1_aim = vector(4, 0)
player1_body = []
player1_head = "It looks like I'm assigning here."

def draw():
    "Advance player and draw game."
    print("xy: ", player1_xy)
    print("head: ", player1_head)
    print("body: ", player1_body)
    player1_xy.move(player1_aim)
    player1_head = player1_xy.copy()
    player1_body.append(player1_head)
    square(player1_xy.x, player1_xy.y, 3, 'red')
    update()
    ontimer(draw, 200)

setup(420, 420, 370, 0)
hideturtle()
tracer(False)

draw()
done()

Because you failed to declare player1_head as a global , in the draw() function it appears to that function that you're printing out local variable player1_head before it has a value: 因为您未能将player1_head声明为global ,所以在draw()函数中,该函数似乎正在打印具有值的局部变量player1_head

print("head: ", player1_head)
# ...
player1_head = player1_xy.copy()

Instead do: 而是:

def draw():
    """ Advance player and draw game. """

    global player1_head

    print("xy: ", player1_xy)
    print("head: ", player1_head)
    print("body: ", player1_body)
    player1_xy.move(player1_aim)
    player1_head = player1_xy.copy()
    player1_body.append(player1_head)

    square(player1_xy.x, player1_xy.y, 3, 'red')

    update()
    ontimer(draw, 200)

The assignment player1_head = player1_xy.copy() in the draw() function is saying to Python that the variable player1_head is a local variable to the function draw() , and since print("head: ", player1_head) is referencing a local variable before its assignment, the error is shown. draw()函数中的分配player1_head = player1_xy.copy()对Python来说,变量player1_head是函数draw()的局部变量,并且由于print("head: ", player1_head)引用了局部变量在分配之前,将显示错误。 You can fix this by using player1_head as a global variable (since you're modifying it, same goes for the variable player1_body , since you're doing player1_body.append(player1_head) ), like so: 您可以通过将player1_head用作全局变量来解决此问题(因为您正在对其进行修改,因此变量player1_body ,因为您正在执行player1_body.append(player1_head) ),如下所示:

def draw():
    "Advance player and draw game."
    global player1_head
    #...rest of the code

Note however that you should avoid using global variables when possible, this is one the problems that arises from using them (they can be modified by any function, which can sometimes lead to errors and confusions). 但是请注意,应尽可能避免使用全局变量,这是使用它们引起的问题(可以通过任何函数修改它们,有时可能会导致错误和混乱)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UnBoundLocalError:赋值之前引用的局部变量(Python) - UnBoundLocalError: local variable referenced before assignment (Python) python - UnboundLocalError:分配前引用的局部变量 - python - UnboundLocalError: local variable referenced before assignment Python-UnboundLocalError:分配前引用的局部变量 - Python - UnboundLocalError: local variable referenced before assignment` Python | 如果变量:| UnboundLocalError:赋值前引用的局部变量'variable' - Python | if variable: | UnboundLocalError: local variable 'variable' referenced before assignment UnboundLocalError:在python闭包中赋值之前引用的局部变量 - UnboundLocalError: local variable referenced before assignment in python closure Python(3.3):UnboundLocalError:分配前已引用局部变量 - Python (3.3): UnboundLocalError: local variable referenced before assignment Python 分裂问题 UnboundLocalError:分配前引用了局部变量“e” - Python Splinter issue UnboundLocalError: local variable 'e' referenced before assignment UnboundLocalError:分配前引用的局部变量“转” - python - UnboundLocalError: local variable 'turn' referenced before assignment - python 如何修复 UnboundLocalError:在 Python 中分配之前引用的局部变量“df” - How to fix UnboundLocalError: local variable 'df' referenced before assignment in Python UnboundLocalError:在 Python3 中赋值之前引用了局部变量“sumOfOdd” - UnboundLocalError: local variable 'sumOfOdd' referenced before assignment in Python3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM