[Tutor] Problem with turtle

Alan Gauld alan.gauld at btinternet.com
Fri Mar 12 01:20:55 CET 2010

"Marco Rompré" <marcodrompre at gmail.com> wrote

> turtle was drawing a yellow square then it was drawing a
> triangle on the sqare but with no lines whatsovever

You'll need to put the pen down() again for it to draw
the lines...

> like it was just going over it and the last problem was
> that it was never stopping

Thats because you increment n outside the while loop
so it never stops looping.

> I hope I am more precis with my explanations.

Much better, thanks.

> n=0
> while n < 10 :
>   down()                         # abaisser le crayon
>   carre(25, 'yellow', 0)         # tracer un carré
>   up()
>   forward(30)
>   triangle(90, 'blue',0)   # THE PEN IS STILL UP HERE
> n = n + 1        # THIS IS OUTSIDE THE WHILE BLOCK
>

I notice this pattern in your functions too. You are using a
while loop to repeat a fixed number of times. A for loop
is the normal way to do that and is guaranteed to stop...:
For example your square function has:

c=0
while c<4:
left(angle)
forward(taille)
right(90)
c = c+1

which can be written:

for c in range(4):
left(angle)
forward(taille)
right(90)

But notice the left and right kind of cancel out so
you usually won't get a square... I suspect you
meant to have:

left(angle)
for c in range(4):
forward(taille)
right(90)

Similarly for the triangle I think you want:

left(angle)
for c in range(3):
forward(taille)
right(120)

When working with the turtle its a good idea to test your
functions at the interactive prompt (>>>). Put the functions
in a file and import the file then you can call them directly
and see whether they draw what you expect.

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/