Download Python for Kids Solutions PDF

TitlePython for Kids Solutions
File Size1.5 MB
Total Pages48
Document Text Contents
Page 1

SolutionS to
Programming PuzzleS

Here are the solutions to the programming puzzles at
the ends of the chapters. There’s not always a single
solution to a puzzle, so the one you’ve come up with
may not match what you’ll find here, but the examples
will give you an idea of possible approaches.

Page 2

2 Solutions for Chapter 3

Chapter 3
#1: Favorites
Here’s one solution with three favorite hobbies and three favorite
foods:

>>> hobbies = ['Pokemon', 'LEGO Mindstorms', 'Mountain Biking']
>>> foods = ['Pancakes', 'Chocolate', 'Apples']
>>> favorites = hobbies + foods
>>> print(favorites)
['Pokemon', 'LEGO Mindstorms', 'Mountain Biking', 'Pancakes',
'Chocolate', 'Apples']

#2: Counting Combatants
We can do the calculation in a number of different ways. We have
three buildings with 25 ninjas hiding on each roof, and two tunnels
with 40 samurai hiding in each. We could work out the total ninjas
and then the total samurai, and just add those two numbers together:

>>> 3*25
75
>>> 2*40
80
>>> 75+80
155

Much shorter (and better) is to combine those three equations,
using parentheses (the parentheses aren’t necessary, due to the
order of the mathematical operations, but they make the equation
easier to read):

>>> (3*25)+(2*40)

But perhaps a nicer Python program would be something like
the following, which tells us what we’re calculating:

>>> roofs = 3
>>> ninjas_per_roof = 25
>>> tunnels = 2
>>> samurai_per_tunnel = 40
>>> print((roofs * ninjas_per_roof) + (tunnels * samurai_per_tunnel))
155

Page 24

24 Solutions for Chapter 12

A triangle has three points, which means three sets of x and y
coordinates. We can use the randrange function in the random module
(as in the random rectangle example in Chapter 12), to randomly
generate the coordinates for the three points (six numbers in total).
We can then use the random_triangle function to draw the triangle.

>>> def random_triangle():
p1 = random.randrange(w)
p2 = random.randrange(h)
p3 = random.randrange(w)
p4 = random.randrange(h)
p5 = random.randrange(w)
p6 = random.randrange(h)
canvas.create_polygon(p1, p2, p3, p4, p5, p6, \
fill="", outline="black")

Finally, we create a loop to draw a whole bunch of random
triangles.

>>> for x in range(0, 100):
random_triangle()

This results in something like the following:

To fill the window with random colored triangles, first create a
list of colors. We can add this to the setup code at the beginning of
the program.

Page 25

Solutions for Chapter 12 25

>>> from tkinter import *
>>> import random
>>> w = 400
>>> h = 400
>>> tk = Tk()
>>> canvas = Canvas(tk, width=w, height=h)
>>> canvas.pack()
>>> colors = ['red','green','blue','yellow','orange','white','purple']

We can then use the choice function of the random module to
randomly pick an item from this list of colors, and use it in the call
to create_polygon:

def random_triangle():
p1 = random.randrange(w)
p2 = random.randrange(h)
p3 = random.randrange(w)
p4 = random.randrange(h)
p5 = random.randrange(w)
p6 = random.randrange(h)
color = random.choice(colors)
canvas.create_polygon(p1, p2, p3, p4, p5, p6, \
fill=color, outline="")

Suppose we loop 100 times again:

>>> for x in range(0, 100):
random_triangle()

The result will be something like these triangles:

Page 47

Solutions for Chapter 18 47

elif self.x < 0 and co.x1 <= 0:
self.x = 0
left = False
for sprite in self.game.sprites:
if sprite == self:
continue
sprite_co = sprite.coords()
if top and self.y < 0 and collided_top(co, sprite_co):
self.y = -self.y
top = False
if bottom and self.y > 0 and collided_bottom(self.y, \
co, sprite_co):
self.y = sprite_co.y1 - co.y2
if self.y < 0:
self.y = 0
bottom = False
top = False
if bottom and falling and self.y == 0 \
and co.y2 < self.game.canvas_height \
and collided_bottom(1, co, sprite_co):
falling = False
if left and self.x < 0 and collided_left(co, sprite_co):
self.x = 0
left = False
if sprite.endgame:
self.end(sprite)
if right and self.x > 0 \
and collided_right(co, sprite_co):
self.x = 0
right = False
if sprite.endgame:
self.end(sprite)
if falling and bottom and self.y == 0 \
and co.y2 < self.game.canvas_height:
self.y = 4
self.game.canvas.move(self.image, self.x, self.y)

def end(self, sprite):
self.game.running = False
sprite.opendoor()
time.sleep(1)
self.game.canvas.itemconfig(self.image, state='hidden')
sprite.closedoor()

Page 48

48 Solutions for Chapter 18

g = Game()
platform1 = PlatformSprite(g, PhotoImage(file="platform1.gif"), \
0, 480, 100, 10)
platform2 = PlatformSprite(g, PhotoImage(file="platform1.gif"), \
150, 440, 100, 10)
platform3 = PlatformSprite(g, PhotoImage(file="platform1.gif"), \
300, 400, 100, 10)
platform4 = PlatformSprite(g, PhotoImage(file="platform1.gif"), \
300, 160, 100, 10)
platform5 = MovingPlatformSprite(g, PhotoImage(file="platform2.gif"),\
175, 350, 66, 10)
platform6 = PlatformSprite(g, PhotoImage(file="platform2.gif"), \
50, 300, 66, 10)
platform7 = PlatformSprite(g, PhotoImage(file="platform2.gif"), \
170, 120, 66, 10)
platform8 = PlatformSprite(g, PhotoImage(file="platform2.gif"), \
45, 60, 66, 10)
platform9 = MovingPlatformSprite(g, PhotoImage(file="platform3.gif"),\
170, 250, 32, 10)
platform10 = PlatformSprite(g, PhotoImage(file="platform3.gif"), \
230, 200, 32, 10)
g.sprites.append(platform1)
g.sprites.append(platform2)
g.sprites.append(platform3)
g.sprites.append(platform4)
g.sprites.append(platform5)
g.sprites.append(platform6)
g.sprites.append(platform7)
g.sprites.append(platform8)
g.sprites.append(platform9)
g.sprites.append(platform10)
door = DoorSprite(g, 45, 30, 40, 35)
g.sprites.append(door)
sf = StickFigureSprite(g)
g.sprites.append(sf)
g.mainloop()

Similer Documents