如何解决用作功能参数不起作用
import turtle
t = turtle.Turtle()
def vary(shape,dup):
while not out_of_bound():
shape
dup
def out_of_bound():#returns True if turtle is out of screen
out = False
height = (turtle.window_height()-30)/2
width = (turtle.window_width()-30)/2
if abs(t.xcor())>width or abs(t.ycor())>height:
out = True
return out
def linear(direction,distance):#for linear duplication
try:
origin_head = t.heading()
t.seth(direction)
t.up()
t.fd(distance)
t.seth(origin_head)#ensures duplication remains linear
except:
print('Invalid input.')
def circle(radius,steps=None,orient=None,circle_color=None,shape_color=None):#circle or shape in circle,orientation in degrees
try:
t.down()
t.circle(-radius)
t.up()
t.circle(-radius,orient)#set orientation of the shape in circle
t.down()
t.circle(-radius,steps=steps)#draw shape in the circle
t.up()
t.lt(180)
t.circle(radius,orient)#return to default position
t.seth(0)
except:
print('Invalid input.')`enter code here`
以上代码是我尝试使用乌龟库以线性方式创建模式的方法。当我调用该函数时,variable(circle(50,4,45),linear(0,100))while循环仅绘制一种形状并停止,而代码继续运行。请帮忙。
解决方法
像这样调用vary
不会将circle
和linear
作为参数传递给vary
。 vary
在这种情况下将收到的是那些函数的返回值(在这种情况下,两者均为None
)。如果要将函数作为参数传递,请不要添加括号,否则会导致调用函数,并返回其返回值。
由于上述原因,下面的代码:
while not out_of_bound():
shape
dup
等效于此:
while not out_of_bound():
None
None
显然什么也没做。
以下代码应该可以实现您的目标:
def vary(shape,shape_args,dup,dup_args):
while not out_of_bound():
shape(*shape_args)
dup(*dup_args)
然后这样称呼它:vary(circle,(50,4,45),linear,(0,100))
。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。