[Tutor] calling function
alan.gauld at yahoo.co.uk
Mon Nov 27 19:19:50 EST 2017
On 27/11/17 20:47, Howard Lawrence wrote:
> import turtle
> # this part draws a square
> def square():
> my_turtle = turtle.Turtle()
Note that this creates my_turtle as a local
variable inside the function. It will not
be visible outside the function.
So this should fail; with an error message
- a name error most likely.
> # this is a second square
And this creates a new square directly on top of
the earlier one, so they look like one.
You need to create the turtle outside the function
and then refer to it from inside the function,
ideally by passing it in as an argument:
my_turtle = turtle.Turtle()
Author of the Learn to Program web site
Follow my photo-blog on Flickr at:
More information about the Tutor