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

Discussion in 'Python' started by Gregor Lingl, Nov 30, 2009.

  1. Gregor Lingl

    Gregor Lingl Guest

    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
    > <mailto:>
    > http://web.bryant.edu/~bblais <http://web.bryant.edu/%7Ebblais>
    >
    >
    >
    > ------------------------------------------------------------------------
    >
    > _______________________________________________
    > Edu-sig mailing list
    >
    > http://mail.python.org/mailman/listinfo/edu-sig
    >
     
    Gregor Lingl, Nov 30, 2009
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Rush
    Replies:
    1
    Views:
    384
    Paul Boddie
    Dec 20, 2006
  2. matchstick86

    sig : process vs. process(sig)

    matchstick86, Oct 12, 2009, in forum: VHDL
    Replies:
    1
    Views:
    579
    power_hf2005
    Oct 13, 2009
  3. Edward Cherlin
    Replies:
    1
    Views:
    633
    Alf P. Steinbach
    Nov 30, 2009
  4. kirby urner
    Replies:
    0
    Views:
    458
    kirby urner
    Nov 30, 2009
  5. Colin J. Williams
    Replies:
    0
    Views:
    118
    Colin J. Williams
    May 4, 2013
Loading...

Share This Page