如何修复我分配的未分配变量的 UnboundLocalError?
我正在尝试使用 Turtle 库和递归在 Python 中创建 Koch 雪花生成器。我有一个包含十六进制代码的字符串列表,我使用变量 color
让笔按照列表的顺序更改颜色。正如您所看到的,我在函数定义之外定义了 color
,但是当我尝试运行它时,我收到 UnboundLocalError ,指出 color
在分配之前已被引用。如何修复错误以便我的代码能够按预期运行?
这是我编写的一些用于生成雪花的代码。每次绘制一条线时,color
都会增加一,因此下一次绘制线时,使用的颜色是 palette[color % 6]
import turtle
turtle.colormode(255)
palette = ["#ff0f7b","#fd445d","#fc5552","#fa8139","#f98a34","#f89b29"]
color = 0
# Draw a Koch curve
def koch(iteration, length):
if iteration == 1:
turtle.forward(length/3)
turtle.pencolor(palette[color % 6])
color += 1
else:
koch(iteration-1, length/3)
turtle.left(60)
这里是我收到的错误消息:
Traceback (most recent call last):
File "C:\Users\hummi\Python Files\koch_snowflake.py", line 49, in <module>
koch_snowflake(4, 480)
File "C:\Users\hummi\Python Files\koch_snowflake.py", line 41, in koch_snowflake
koch(iteration, length)
File "C:\Users\hummi\Python Files\koch_snowflake.py", line 15, in koch
koch(iteration-1, length/3)
File "C:\Users\hummi\Python Files\koch_snowflake.py", line 15, in koch
koch(iteration-1, length/3)
File "C:\Users\hummi\Python Files\koch_snowflake.py", line 15, in koch
koch(iteration-1, length/3)
File "C:\Users\hummi\Python Files\koch_snowflake.py", line 12, in koch
turtle.pencolor(palette[color % 6])
UnboundLocalError: local variable 'color' referenced before assignment
I am trying to create a Koch snowflake generator in Python using the Turtle library and recursion. I have a list of strings with hex codes in them and I am using the variable color
to have the pen change colors in the order of the list. As you can see, I defined color
outside of the function definition but when I try to run it, I get and UnboundLocalError saying that color
was referenced before it was assigned. How can I fix the error so my code will run as intended?
Here is some of the code I wrote to generate the Snowflake. color
is supposed to increase by one every time a line is drawn, so then the next time a line is drawn the color used is a palette[color % 6]
import turtle
turtle.colormode(255)
palette = ["#ff0f7b","#fd445d","#fc5552","#fa8139","#f98a34","#f89b29"]
color = 0
# Draw a Koch curve
def koch(iteration, length):
if iteration == 1:
turtle.forward(length/3)
turtle.pencolor(palette[color % 6])
color += 1
else:
koch(iteration-1, length/3)
turtle.left(60)
here is the error message I am getting:
Traceback (most recent call last):
File "C:\Users\hummi\Python Files\koch_snowflake.py", line 49, in <module>
koch_snowflake(4, 480)
File "C:\Users\hummi\Python Files\koch_snowflake.py", line 41, in koch_snowflake
koch(iteration, length)
File "C:\Users\hummi\Python Files\koch_snowflake.py", line 15, in koch
koch(iteration-1, length/3)
File "C:\Users\hummi\Python Files\koch_snowflake.py", line 15, in koch
koch(iteration-1, length/3)
File "C:\Users\hummi\Python Files\koch_snowflake.py", line 15, in koch
koch(iteration-1, length/3)
File "C:\Users\hummi\Python Files\koch_snowflake.py", line 12, in koch
turtle.pencolor(palette[color % 6])
UnboundLocalError: local variable 'color' referenced before assignment
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只需要添加一个
全局颜色
语句,如下所示:如果没有添加,修改
color
的值将使其成为本地范围的变量。由于在该局部范围内,它尚未被分配,并且增量语句需要变量的先前值,因此您会收到所看到的错误。添加global color
语句会强制该语句引用全局color
值,即使它正在修改该值。如果您仅读取color
,则不需要global color
语句。这里有一个有点奇怪的 Python 事实。You just need to add a
global color
statement, like this:Without that addition, modifying the value of
color
makes it a locally scoped variable. Since in that local scope, it hasn't been assigned to yet, and the increment statement requires a previous value for the variable, you get the error that you are seeing. Adding theglobal color
statement forces the statement to refer to the globalcolor
value, even though it is modifying the value. If you were only reading fromcolor
, theglobal color
statement wouldn't be necessary. A slightly strange Python factoid here.我的倾向是循环颜色,而不是使用全局索引和模块化算术:
更改调色板以使颜色变化更加明显:
My inclination would be to cycle the colors rather than use a global index and modular arithmetic:
Pallette changed to make color changes more obvious: