Invalid syntax for turtle name (python) -
i trying write program draw flower, no matter keeps throwing "invalid syntax" error turtle name. have taken out of other code, tried naming turtle different, yet nothing works. ideas?
import turtle def draw_flower(): window = turtle.screen() window.bgcolor(#42dff4) sam = turtle.turtle() sam.forward(50) window.exitonclick() draw_flower()
besides quoting color string, noted in comments, lines of code in wrong order. example, nothing should follow window.exitonclick()
:
window.exitonclick() draw_flower()
make (or window.mainloop()
) last statement of program that's when code ends , tk event handler loop begins. i.e. reverse order of these 2 statements. second problem variable window
in wrong scope:
def draw_flower(): window = turtle.screen() ... window.exitonclick()
since it's defined locally in draw_flower()
, it's not available use globally. here's rework of code addressing both issues:
import turtle def draw_flower(): sam = turtle.turtle() sam.forward(50) window = turtle.screen() window.bgcolor("#42dff4") draw_flower() window.exitonclick()
Comments
Post a Comment