4.11. Exercises¶
-
Write a program that prints
We like Python's turtles!
100 times.(ex_3_1)
31for i in range(100):
2print("We like Python's turtles!")
3
(pt_q1_answer)
-
- Write a program that uses a for loop to print
One of the months of the year is January
One of the months of the year is February
One of the months of the year is March
etc …
(ex_3_3)
31for amonth in ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'November', 'December']:
2print("One of the months of the year is", amonth)
3
(pt_q3_answer)
-
Assume you have a list of numbers
12, 10, 32, 3, 66, 17, 42, 99, 20
Write a loop that prints each of the numbers on a new line.
Write a loop that prints each number and its square on a new line.
(ex_3_4)
-
Use
for
loops to make a turtle draw these regular polygons (regular means all sides the same lengths, all angles the same):An equilateral triangle
A square
A hexagon (six sides)
An octagon (eight sides)
(ex_3_5)
# draw an equilateral triangle import turtle wn = turtle.Screen() norvig = turtle.Turtle() for i in range(3): norvig.forward(100) # the angle of each vertice of a regular polygon # is 360 divided by the number of sides norvig.left(360/3) wn.exitonclick()
# draw a square import turtle wn = turtle.Screen() kurzweil = turtle.Turtle() for i in range(4): kurzweil.forward(100) kurzweil.left(360/4) wn.exitonclick()
# draw a hexagon import turtle wn = turtle.Screen() dijkstra = turtle.Turtle() for i in range(6): dijkstra.forward(100) dijkstra.left(360/6) wn.exitonclick()
# draw an octogon import turtle wn = turtle.Screen() knuth = turtle.Turtle() for i in range(8): knuth.forward(75) knuth.left(360/8) wn.exitonclick()
-
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.
(ex_3_6)
-
A drunk pirate makes a random turn and then takes 100 steps forward, makes another random turn, takes another 100 steps, turns another random amount, etc. A social science student records the angle of each turn before the next 100 steps are taken. Her experimental data is
160, -43, 270, -97, -43, 200, -940, 17, -86
. (Positive angles are counter-clockwise.) Use a turtle to draw the path taken by our drunk friend. After the pirate is done walking, print the current heading.(ex_3_7)
231import turtle
2
3wn = turtle.Screen()
4lovelace = turtle.Turtle()
5
6# move the turtle forward a little so that the whole path fits on the screen
7lovelace.penup()
8lovelace.forward(60)
9
10# now draw the drunk pirate's path
11lovelace.pendown()
12for angle in [160, -43, 270, -97, -43, 200, -940, 17, -86]:
13
14# we use .left() so that positive angles are counter-clockwise
15# and negative angles are clockwise
16lovelace.left(angle)
17lovelace.forward(100)
18
19# the .heading() method gives us the turtle's current heading in degrees
20print("The pirate's final heading was", lovelace.heading())
21
22wn.exitonclick()
23
(pt_q7_answer)
-
On a piece of scratch paper, trace the following program and show the drawing. When you are done, press
run
and check your answer.(ex_3_8)
-
Write a program to draw a shape like this:
(ex_3_9)
81import turtle
2
3turing = turtle.Turtle()
4
5for i in range(5):
6turing.forward(110)
7turing.left(216)
8
(pt_q9_answer)
-
Write a program to draw a face of a clock that looks something like this:
(ex_3_10)
-
Write a program to draw some kind of picture. Be creative and experiment with the turtle methods provided in Summary of Turtle Methods.
(ex_3_11)
111import turtle
2
3tanenbaum = turtle.Turtle()
4
5tanenbaum.hideturtle()
6tanenbaum.speed(20)
7
8for i in range(350):
9tanenbaum.forward(i)
10tanenbaum.right(98)
11
(pt_q11_answer)
-
Create a turtle and assign it to a variable. When you print its type, what do you get?
(ex_3_12)
-
A sprite is a simple spider shaped thing with n legs coming out from a center point. The angle between each leg is 360 / n degrees.
Write a program to draw a sprite where the number of legs is provided by the user.
(ex_3_13)
251import turtle
2
3wn = turtle.Screen()
4
5babbage = turtle.Turtle()
6babbage.shape("triangle")
7
8n = int(input("How many legs should this sprite have? "))
9angle = 360 / n
10
11for i in range(n):
12# draw the leg
13babbage.right(angle)
14babbage.forward(65)
15babbage.stamp()
16
17# go back to the middle and turn back around
18babbage.right(180)
19babbage.forward(65)
20babbage.right(180)
21
22babbage.shape("circle")
23
24wn.exitonclick()
25
(pt_q13_answer)