Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

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

 
 
Gregor Lingl
Guest
Posts: n/a
 
      11-30-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/20168...ector?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
> http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)>
> http://web.bryant.edu/~bblais <http://web.bryant.edu/%7Ebblais>
>
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Edu-sig mailing list
> (E-Mail Removed)
> http://mail.python.org/mailman/listinfo/edu-sig
>

 
Reply With Quote
 
 
 
Reply

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



Advertisments