python - Make function recursive -
i trying convert function below recursive 1 keep getting error when try it.
typeerror: unsupported operand type(s) *: 'float' , 'nonetype'
def turtle_spiral(forward): minus = 8 t = turtle.turtle() t.pendown() t.shape("turtle") #while forward > 10: randm = random.randrange(5) colours = ["blue", "orange", "yellow", "green", "purple","black","red","pink"] t.goto(-100,0) if forward <= 10: return false else: t.color(colours[randm]) #t.speed(10) t.fd(turtle_spiral(forward*minus)) #t.circle(forward, 360) t.right(90) #forward -= minus turtle_spiral(100) wd.mainloop()
i've had go @ modifying code think want do:
import turtle import random def turtle_spiral(forward): minus = 8 randm = random.randrange(5) colours = ["blue", "orange", "yellow", "green", "purple","black","red","pink"] if forward <= 10: return 0 else: t.color(colours[randm]) t.right(90) t.fd(forward) return turtle_spiral(forward-minus) t = turtle.turtle() t.pendown() t.shape("turtle") turtle_spiral(100)
the key recursion defining end point(s) of recursion. in case, when turtle being asked go forward distance of less 10. @ end point recursive function should not call , return.
in other cases want function call @ point argument gets closer towards end-point. in case argument forward-minus
, value closer , closer end condition more turtle_spiral
calls recursively.
Comments
Post a Comment