Book Exercises

Spins with compass


In [1]:
spins = input("How many times did you spin? (Enter a negative number for couter-clockwise spins) ")

# Need to ensure the direction of the spin is always correct.
# This works because of the definition of the modulo operator and what happens with
# negative numbers.   Keep this in mind for the assignment for chapter 3.
degrees = (float(spins) * 360) % 360

print("You are facing", degrees, "degrees relative to north")


How many times did you spin? (Enter a negative number for couter-clockwise spins) -10
You are facing 0.0 degrees relative to north

Fun with Turtles


In [2]:
import turtle               
wn = turtle.Screen()        # creates a graphics window
alex = turtle.Turtle()      # create a turtle named alex
alex.speed(1)
alex.shape('turtle')

for i in [0,1,2,3,4,5]:
    alex.forward(150)           # tell alex to move forward by 150 units
    alex.left(85)               # turn by 90 degrees
    alex.forward(75)

## This won't run as expected with the notebook. Wait a moment for the window to be created.

wn.exitonclick()

Looking at documentation for tutles

The documentation for the module inside of Python can be found here: https://docs.python.org/3.6/library/turtle.html


In [1]:
# Copied from the documentation example. This is bad practice to do import * DO NOT DO IT!
from turtle import *
color('red', 'yellow')
begin_fill()
while True:
    forward(200)
    left(170)
    if abs(pos()) < 1:
        break
end_fill()
done()

Using Range


In [8]:
for number in range(6, 0, -1):
    print("I have", number, "cookies.  Iím going to eat one.")

print('I ate all my cookies')


I have 6 cookies.  Iím going to eat one.
I have 5 cookies.  Iím going to eat one.
I have 4 cookies.  Iím going to eat one.
I have 3 cookies.  Iím going to eat one.
I have 2 cookies.  Iím going to eat one.
I have 1 cookies.  Iím going to eat one.
I ate all my cookies

Lots of turtles


In [1]:
import turtle
wn = turtle.Screen()
wn.bgcolor("lightgreen")
tess = turtle.Turtle()
tess.color("blue")
tess.shape("turtle")

jim = turtle.Turtle()
jim.color("green")
jim.shape("turtle")

carl = turtle.Turtle()
carl.color("red")
carl.shape("turtle")

tess.up()
carl.up()
jim.up()
# Keep in mind for today's studio
for size in range(5, 60, 2):    # start with size = 5 and grow by 2
    tess.stamp()                # leave an impression on the canvas
    carl.stamp()
    jim.stamp()
    
    carl.forward(size + 10)
    jim.forward(size)
    tess.forward(size)          # move tess along
    
    carl.right(90)
    jim.left(24)
    tess.right(24)              # and turn her

wn.exitonclick()


range(5, 60, 2)

Math


In [8]:
from math import sqrt

print(sqrt(24))
print(sqrt(25))

print(sqrt(-2))


4.898979485566356
5.0
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-8-45a521cbeb11> in <module>()
      4 print(sqrt(25))
      5 
----> 6 print(sqrt(-2))

ValueError: math domain error

Random

Remember how it's not actually random. Here is an example:


In [4]:
import random

random.seed(5)

print(random.randint(0,10))
print(random.randint(0,10))
print(random.randint(0,10), '\n')

random.seed(5)

print(random.randint(0,10))
print(random.randint(0,10))
print(random.randint(0,10), '\n')

random.seed(5)

print(random.randint(0,10))
print(random.randint(0,10))
print(random.randint(0,10), '\n')

random.seed(5)

print(random.randint(0,10))
print(random.randint(0,10))
print(random.randint(0,10), '\n')


9
4
5 

9
4
5 

9
4
5 

9
4
5 

Chapter 4 Exercises

Bottles of beer


In [6]:
bottles_of_beer = 99

for bottle_number in range(bottles_of_beer, 0, -1):
    print(bottle_number, "Bottles of Beer on the Wall")
    print("Take one down pass it around")


99 Bottles of Beer on the Wall
Take one down pass it around
98 Bottles of Beer on the Wall
Take one down pass it around
97 Bottles of Beer on the Wall
Take one down pass it around
96 Bottles of Beer on the Wall
Take one down pass it around
95 Bottles of Beer on the Wall
Take one down pass it around
94 Bottles of Beer on the Wall
Take one down pass it around
93 Bottles of Beer on the Wall
Take one down pass it around
92 Bottles of Beer on the Wall
Take one down pass it around
91 Bottles of Beer on the Wall
Take one down pass it around
90 Bottles of Beer on the Wall
Take one down pass it around
89 Bottles of Beer on the Wall
Take one down pass it around
88 Bottles of Beer on the Wall
Take one down pass it around
87 Bottles of Beer on the Wall
Take one down pass it around
86 Bottles of Beer on the Wall
Take one down pass it around
85 Bottles of Beer on the Wall
Take one down pass it around
84 Bottles of Beer on the Wall
Take one down pass it around
83 Bottles of Beer on the Wall
Take one down pass it around
82 Bottles of Beer on the Wall
Take one down pass it around
81 Bottles of Beer on the Wall
Take one down pass it around
80 Bottles of Beer on the Wall
Take one down pass it around
79 Bottles of Beer on the Wall
Take one down pass it around
78 Bottles of Beer on the Wall
Take one down pass it around
77 Bottles of Beer on the Wall
Take one down pass it around
76 Bottles of Beer on the Wall
Take one down pass it around
75 Bottles of Beer on the Wall
Take one down pass it around
74 Bottles of Beer on the Wall
Take one down pass it around
73 Bottles of Beer on the Wall
Take one down pass it around
72 Bottles of Beer on the Wall
Take one down pass it around
71 Bottles of Beer on the Wall
Take one down pass it around
70 Bottles of Beer on the Wall
Take one down pass it around
69 Bottles of Beer on the Wall
Take one down pass it around
68 Bottles of Beer on the Wall
Take one down pass it around
67 Bottles of Beer on the Wall
Take one down pass it around
66 Bottles of Beer on the Wall
Take one down pass it around
65 Bottles of Beer on the Wall
Take one down pass it around
64 Bottles of Beer on the Wall
Take one down pass it around
63 Bottles of Beer on the Wall
Take one down pass it around
62 Bottles of Beer on the Wall
Take one down pass it around
61 Bottles of Beer on the Wall
Take one down pass it around
60 Bottles of Beer on the Wall
Take one down pass it around
59 Bottles of Beer on the Wall
Take one down pass it around
58 Bottles of Beer on the Wall
Take one down pass it around
57 Bottles of Beer on the Wall
Take one down pass it around
56 Bottles of Beer on the Wall
Take one down pass it around
55 Bottles of Beer on the Wall
Take one down pass it around
54 Bottles of Beer on the Wall
Take one down pass it around
53 Bottles of Beer on the Wall
Take one down pass it around
52 Bottles of Beer on the Wall
Take one down pass it around
51 Bottles of Beer on the Wall
Take one down pass it around
50 Bottles of Beer on the Wall
Take one down pass it around
49 Bottles of Beer on the Wall
Take one down pass it around
48 Bottles of Beer on the Wall
Take one down pass it around
47 Bottles of Beer on the Wall
Take one down pass it around
46 Bottles of Beer on the Wall
Take one down pass it around
45 Bottles of Beer on the Wall
Take one down pass it around
44 Bottles of Beer on the Wall
Take one down pass it around
43 Bottles of Beer on the Wall
Take one down pass it around
42 Bottles of Beer on the Wall
Take one down pass it around
41 Bottles of Beer on the Wall
Take one down pass it around
40 Bottles of Beer on the Wall
Take one down pass it around
39 Bottles of Beer on the Wall
Take one down pass it around
38 Bottles of Beer on the Wall
Take one down pass it around
37 Bottles of Beer on the Wall
Take one down pass it around
36 Bottles of Beer on the Wall
Take one down pass it around
35 Bottles of Beer on the Wall
Take one down pass it around
34 Bottles of Beer on the Wall
Take one down pass it around
33 Bottles of Beer on the Wall
Take one down pass it around
32 Bottles of Beer on the Wall
Take one down pass it around
31 Bottles of Beer on the Wall
Take one down pass it around
30 Bottles of Beer on the Wall
Take one down pass it around
29 Bottles of Beer on the Wall
Take one down pass it around
28 Bottles of Beer on the Wall
Take one down pass it around
27 Bottles of Beer on the Wall
Take one down pass it around
26 Bottles of Beer on the Wall
Take one down pass it around
25 Bottles of Beer on the Wall
Take one down pass it around
24 Bottles of Beer on the Wall
Take one down pass it around
23 Bottles of Beer on the Wall
Take one down pass it around
22 Bottles of Beer on the Wall
Take one down pass it around
21 Bottles of Beer on the Wall
Take one down pass it around
20 Bottles of Beer on the Wall
Take one down pass it around
19 Bottles of Beer on the Wall
Take one down pass it around
18 Bottles of Beer on the Wall
Take one down pass it around
17 Bottles of Beer on the Wall
Take one down pass it around
16 Bottles of Beer on the Wall
Take one down pass it around
15 Bottles of Beer on the Wall
Take one down pass it around
14 Bottles of Beer on the Wall
Take one down pass it around
13 Bottles of Beer on the Wall
Take one down pass it around
12 Bottles of Beer on the Wall
Take one down pass it around
11 Bottles of Beer on the Wall
Take one down pass it around
10 Bottles of Beer on the Wall
Take one down pass it around
9 Bottles of Beer on the Wall
Take one down pass it around
8 Bottles of Beer on the Wall
Take one down pass it around
7 Bottles of Beer on the Wall
Take one down pass it around
6 Bottles of Beer on the Wall
Take one down pass it around
5 Bottles of Beer on the Wall
Take one down pass it around
4 Bottles of Beer on the Wall
Take one down pass it around
3 Bottles of Beer on the Wall
Take one down pass it around
2 Bottles of Beer on the Wall
Take one down pass it around
1 Bottles of Beer on the Wall
Take one down pass it around

Cool sample by: Murial


In [ ]:
import turtle
import random
wn = turtle.Screen()
anaise = turtle.Turtle()
hour = 1
lines = 1
angle = 1

anaise.speed(0)
# Set the color mode so the mac is happy.
# wn.colormode(255)

#change starting point of line randomly
while lines < 200:
    anaise.goto(random.randrange(50), random.randrange(50))
    anaise.down()
    angle = (random.randrange(360))
    anaise.color(random.randrange(255),random.randrange(255),random.randrange(255))
    anaise.pensize(random.randrange(11))
    anaise.right(angle)
    anaise.forward(random.randrange(100))
    anaise.up()
    
    #Count the number of times the loop occurs
    lines = lines + 1
    
wn.exitonclick()

Draw the regular polygon

Write a program that asks the user for the number of sides, the length of the side, the color, and the fill color of a regular polygon. The program should draw the polygon and then fill it in.


In [ ]: