Reputation: 45
I am trying to write a program to draw a flower, but no matter what I do it keeps throwing an "invalid syntax" error for the turtle name. I have taken out all of my other code, tried naming the turtle something different, yet nothing works. Any ideas?
import turtle
def draw_flower():
window = turtle.Screen()
window.bgcolor(#42dff4)
sam = turtle.Turtle()
sam.forward(50)
window.exitonclick()
draw_flower()
Upvotes: 0
Views: 1399
Reputation: 41925
Besides quoting the color string, as noted in the comments, your lines of code are in the wrong order. For example, generally nothing should follow window.exitonclick()
:
window.exitonclick()
draw_flower()
Make it (or window.mainloop()
) the last statement of your program as that's when your code ends and the Tk event handler loop begins. I.e. reverse the order of these two statements. The second problem is that the variable window
is in the wrong scope:
def draw_flower():
window = turtle.Screen()
...
window.exitonclick()
Since it's defined locally in draw_flower()
, it's not available to use globally. Here's a rework of your 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()
Upvotes: 2