如何修复我分配的未分配变量的 UnboundLocalError?

发布于 2025-01-15 19:27:28 字数 1521 浏览 1 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神妖 2025-01-22 19:27:28

您只需要添加一个全局颜色语句,如下所示:

color = 0

# Draw a Koch curve
def koch(iteration, length):
    global color
    if iteration == 1:
        turtle.forward(length/3)
        turtle.pencolor(palette[color % 6])
        color += 1

如果没有添加,修改color的值将使其成为本地范围的变量。由于在该局部范围内,它尚未被分配,并且增量语句需要变量的先前值,因此您会收到所看到的错误。添加global color 语句会强制该语句引用全局color 值,即使它正在修改该值。如果您仅读取color,则不需要global color 语句。这里有一个有点奇怪的 Python 事实。

You just need to add a global color statement, like this:

color = 0

# Draw a Koch curve
def koch(iteration, length):
    global color
    if iteration == 1:
        turtle.forward(length/3)
        turtle.pencolor(palette[color % 6])
        color += 1

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 the global color statement forces the statement to refer to the global color value, even though it is modifying the value. If you were only reading from color, the global color statement wouldn't be necessary. A slightly strange Python factoid here.

再可℃爱ぅ一点好了 2025-01-22 19:27:28

我的倾向是循环颜色,而不是使用全局索引和模块化算术:

import turtle
from itertools import cycle

# palette = cycle(["#ff0f7b", "#fd445d", "#fc5552", "#fa8139", "#f98a34", "#f89b29"])
palette = cycle(['#ff0000', '#000000', '#00ffff', '#0000ff', '#00ff00'])

# Draw a Koch curve
def koch(iteration, length):

    if iteration == 1:
        turtle.pencolor(next(palette))
        turtle.forward(length)
    else:
        koch(iteration-1, length/3)
        turtle.left(60)
        koch(iteration-1, length/3)
        turtle.right(120)
        koch(iteration-1, length/3)
        turtle.left(60)
        koch(iteration-1, length/3)

koch(4, 300)

turtle.done()

更改调色板以使颜色变化更加明显:

在此处输入图像描述

My inclination would be to cycle the colors rather than use a global index and modular arithmetic:

import turtle
from itertools import cycle

# palette = cycle(["#ff0f7b", "#fd445d", "#fc5552", "#fa8139", "#f98a34", "#f89b29"])
palette = cycle(['#ff0000', '#000000', '#00ffff', '#0000ff', '#00ff00'])

# Draw a Koch curve
def koch(iteration, length):

    if iteration == 1:
        turtle.pencolor(next(palette))
        turtle.forward(length)
    else:
        koch(iteration-1, length/3)
        turtle.left(60)
        koch(iteration-1, length/3)
        turtle.right(120)
        koch(iteration-1, length/3)
        turtle.left(60)
        koch(iteration-1, length/3)

koch(4, 300)

turtle.done()

Pallette changed to make color changes more obvious:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文