Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: [Edu-sig] teaching python using turtle module

Thread Tools

Re: [Edu-sig] teaching python using turtle module

Gregor Lingl
Posts: n/a
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.title("A drunken turtle collecting ...")

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

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

t = Turtle(shape="turtle")
points = 0
while abs(t.xcor()) < 200 and abs(t.ycor()) < 200:
found = None
for c in coins:
if t.distance(c) < 10:
found = c
if 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:

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 ...


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
> Removed) <(E-Mail Removed)>
> <>
> ------------------------------------------------------------------------
> _______________________________________________
> Edu-sig mailing list
> (E-Mail Removed)

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: pynguin-0.13 python turtle graphics application now uses python 3 Lee Harr Python 1 03-18-2013 04:18 AM
Detecting a click on the turtle screen when the turtle isn't doinganything? Adam Funk Python 7 02-06-2013 09:52 PM
Re: [Edu-sig] teaching python using turtle module Edward Cherlin Python 1 11-30-2009 09:36 PM
Re: [Edu-sig] teaching python using turtle module kirby urner Python 0 11-30-2009 01:22 AM
ANN: Python's turtle module: collection of examples + demoViewer Gregor Lingl Python 0 08-04-2009 09:30 AM