[Edu-sig] Turtle

kirby urner kirby.urner at gmail.com
Sun Feb 12 14:47:41 EST 2017


> Our schools in Portland (both public and private) seem to favor using MIT
> Scratch before introducing a lexical language, if
>
The Martian Math one (appended) is deliberately way above their reading
> level but the challenge was simply to take printed sheets with the source
> code and match them with the corresponding applications.  Connect the dots,
> so to speak.
>
>
The code below will not run in Python3 as it's cut and paste from the
Codesters environment, which some new modules (sprites, stage...) to
__builtins__

It's still Python though.

"""
I have one baseball and want to completely surround it with others.
One way: put six around the center one on a table, then three on
top, three on the bottom. With real baseballs, this would be
difficult.

To see an animation of what I'm talking about, check here:
http://www.4dsolutions.net/ocn/graphics/cubanim.gif

For a lot more on the mathematics, check here:
http://oeis.org/A005901
"""
stage.set_background("mars")
sprite = codesters.Sprite("person10")
sprite.go_to(0, -100)
sprite.set_say_color("white") # for speaking

def shell(n):
    """
    input n should be a non-negative integer
    says how many balls in any layer
    """
    if (not isinstance(n, int)) or n < 0:
        raise TypeError # signals we're done!
    if n == 0:
        return 1 # the central ball
    return 10 * n * n + 2  # otherwise, if n > 0

# put in a negative number to stop
while True:  # loop until TypeError is raised by user
    try:
        guess = int(sprite.ask("What layer number? (-1 to quit): >"))
        how_many = shell(guess)
        sprite.say("That layer has " + str(how_many) + " balls in it.")
        stage.wait(3)

    except TypeError:
        sprite.say("OK! Thanks for playing.")
        stage.wait(2)
        break

sprite.say("Bye!")
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/edu-sig/attachments/20170212/14247647/attachment.html>


More information about the Edu-sig mailing list