# [Edu-sig] teaching python using turtle module

Gregor Lingl gregor.lingl at aon.at
Mon Nov 30 17:36:14 CET 2009

```Hello Brian,

I think the most natural use of the if statement (using turtle
graphics) occurs in recursive functions drawing trees,
fractals and the like. This is well known from Logo, where
recursion is the canonical way of doing repetitions. (But
note, that Logo has tail recursion optimizaton!)

If you are not yet ready to use recursion with your students,
probably many  of the problems coming to your mind that need
the examination of conditions  can be solved better by using
a conditional loop (i. e. a while -loop in Python) than by
using mere if statements.

That is not the case however, if you have to perform actions in
the body of a loop, that depend on the current situation.
I did a quick search in my repository of examples and found
a fairly short and simple script that demonstrates, what I mean:
a drunken turtle collecting coins (or whatever) on its random walk.

##### Python script using turtle graphics

from turtle import Screen, Turtle
from random import randint

s = Screen()
s.setup(560,560)
s.title("A drunken turtle collecting ...")

s.tracer(False)
writer = Turtle(visible=False)
writer.penup()
writer.goto(0, -275)

coins = []
for i in range(-4,5):
for j in range(-4, 5):
if i == j == 0:
continue
c = Turtle(shape="circle")
c.color("", "orange")
c.shapesize(0.5)
c.goto(40*i, 40*j)
coins.append(c)
s.tracer(True)

DRUNKENNESS = 45
t = Turtle(shape="turtle")
t.color("black","")
points = 0
while abs(t.xcor()) < 200 and abs(t.ycor()) < 200:
t.forward(5)
t.right(randint(-DRUNKENNESS, DRUNKENNESS))
found = None
for c in coins:
if t.distance(c) < 10:
found = c
break
if found:
found.hideturtle()
coins.remove(found)
t.shapesize(1+points/5., outline=1+points)
points += 1

writer.write("{0} points".format(points),
align="center", font=('Arial', 24, 'bold'))

############## End of script

You can see a screenshot of a run of this script here:

http://www.dropbox.com/gallery/2016850/1/TurtleCollector?h=6b370a

The script could be expanded in several ways, e. g. for
doing statistical investigations or examinig how the
result depends on different parameters like drunkenness etc.
Or you distribute the coins randomly ... Does that alter
the average "harvest"?

Just a suggestion ...

Regards,
Gregor

Brian Blais schrieb:
> Hello,
>
> I was just playing with the turtle module, and thought it was an
> interesting way to augment the introduction to python (I teach college
> students, who haven't had any programming).  It's a great way to
> introduce functions, for-loops, and general program structures.
>
> After a bit of playing, I realized that I couldn't think of many
> examples which use turtle with conditional structures (if- and while-
> statements), or functions that return values, as opposed to
> "procedures" like:
>
> def square(length):
>     forward(length)
>     right(90)
>     forward(length)
>     right(90)
>     forward(length)
>     right(90)
>     forward(length)
>     right(90)
>
>
> If-statements could possibly be used with some sort of random behavior
> (if rand()<0.5 ...).  Are there any other situations, using turtle,
> that these structures would be natural?
>
>
>
> thanks,
>
> Brian Blais
>
> --
> Brian Blais
> bblais at bryant.edu <mailto:bblais at bryant.edu>
> http://web.bryant.edu/~bblais <http://web.bryant.edu/%7Ebblais>
>
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Edu-sig mailing list
> Edu-sig at python.org
> http://mail.python.org/mailman/listinfo/edu-sig
>

```