[英]How can I solve this loop function?
L 36 [L 4 [F 100 R 90] R 10] L 36 [L 4 [F 100 R 90] R 10]
Explanation : This program first loop 36 times inside the closed paranthesis and inside 4 times draw F 100 and R 90 (basic square draw) then right 10 degrees and draw square again.解释:该程序首先在闭括号内循环 36 次,在 4 次内循环绘制 F 100 和 R 90(基本正方形绘制),然后向右 10 度并再次绘制正方形。
I have project so I want to do functions like this but my code does not working.I add F and R functions as an argument to L function but I cant add L function as an argument to L function.How can I solve this?我有项目,所以我想做这样的函数,但我的代码不起作用。我将 F 和 R 函数作为参数添加到 L 函数,但我不能将 L 函数作为参数添加到 L 函数。我该如何解决这个问题?
from turtle import Turtle,Screen从龟进口龟,屏幕
s = Screen() t = Turtle() s = 屏幕() t = 海龟()
def F(value):定义 F(值):
t.forward(value)
def R(value):定义 R(值):
t.right(value)
def L(value1, *args, **kw): def L(value1, *args, **kw):
for i in range(value1):
for func in args:
func(kw[func.__name__])
for i in range(36):对于范围内的 i (36):
L(4, F, R, F=100, R=90)
L(1, R, R=10)
I can do like this and work but I cant in one function.我可以这样做并且可以工作,但我不能在一个功能中使用。
s.exitonclick() s.exitonclick()
Try defining all of your functions as methods inside a class尝试将所有函数定义为类中的方法
for i in range(36)... should be a method as well. for i in range(36)... 也应该是一种方法。
If 36 is a constant, leave it as is.如果 36 是常数,则保持原样。 If it's a var, define it as such for the class.
如果它是一个 var,则为该类定义它。
You could go even further with @property decorators, so you can access the last function as an class atribute.您可以使用@property 装饰器走得更远,因此您可以将最后一个函数作为类属性访问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.