【发布时间】:2019-10-31 04:02:57
【问题描述】:
我正在制作一个使用海龟绘制正方形的函数 draw_square。它需要 (t,side_length) 其中 t 是海龟名称,side_length 是边长。但是,当使用 draw_square(dave,50) 在 thonny 中进行测试时,它说名称“dave”未定义
在创建我的函数之前尝试导入乌龟
import turtle
def draw_square(t, side_length):
"""Use the turtle t to draw a square with side_length."""
t=turtle.Turtle()
t.forward(side_length)
t.right(90)
t.forward(side_length)
t.right(90)
t.forward(side_length)
t.right(90)
t.forward(side_length)
t.right(90)
预期结果:
在给出海龟名称和长度后绘制一个预定长度的正方形。
实际结果:
"Traceback (most recent call last):
File "<pyshell>", line 1, in <module>
NameError: name 'dave' is not defined"
【问题讨论】:
-
你在哪里定义/设置“dave”?
-
在交互式 shell 中的 thonny 中,我正在输入:draw_square(dave,50)
-
这不会设置变量“dave”。
-
哦,我以为这就是你为我创建的函数设置它们的方式——我们刚刚在课堂上介绍了这一点。我会在这个话题上做更多的研究。如果你不介意我问,我该如何设置变量?
标签: python function turtle-graphics