Contents
This notebook is based on "Think Python, 2Ed" by Allen B. Downey
https://greenteapress.com/wp/think-python-2e/
In [1]:
import turtle
# NOTE: Many statements in this notebook are commented out as turtle graphics can have
# in a Jupyter Notebook. You can use uncommented versions in your Python IDE.
#bob = turtle.Turtle()
#print( bob )
#bob.fd( 100 ) # Move forward 100 pixels
#bob.lt( 90 ) # Turn 90 degrees to the left
#bob.fd( 100 ) # Move forward 100 pixels
In [2]:
for i in range(4):
print( 'Hello!' )
To practice loops and turtle graphics, complete the following exercises from the textbook:
square
that takes a parameter named t, which is a turtle. It should use the turtle to draw a square. Write a function call that passes bob
as an argument to square, and then run the program again.length
, to square. Modify the body so length of the sides is length, and then modify the function call to provide a second argument. Run the program again. Test your program with a range of values for length.lt
and rt
make 90-degree turns by default, but you can provide a second argument that specifies the number of degrees. For example, lt(bob, 45)
turns bob
45 degrees to the left. Make a copy of square and change the name to polygon. Add another parameter named n
and modify the body so it draws an n-sided regular polygon. Hint: The exterior angles of an n-sided regular polygon are 360.0/n degrees.circle
that takes a turtle, t
, and radius, r
, as parameters and that draws an approximate circle by invoking polygon with an appropriate length and number of sides. Test your function with a range of values of r
. Hint: figure out the circumference of the circle and make sure that length * n = circumference. Another hint: if bob
is too slow for you, you can speed him up by changing bob.delay
, which is the time between moves, in seconds. bob.delay = 0.01
ought to get him moving.arc
that takes an additional parameter angle
, which determines what fraction of a circle to draw. angle
is in units of degrees, so when angle=360
, arc
should draw a complete circle.If you would like more of a challenge, try these additional exercises:
drawSpiral
that takes a parameter named degrees
that specifies the amount in degrees to draw a spiral.drawStar
that takes a parameter named length
that draws a five-sided star with the specified side length.drawCircles
that takes a parameter named count
that draws the specified number of circles side-by-side.
In [3]:
def square( t ):
for i in range( 4 ):
t.fd( 100 )
t.lt( 90 )
#square( bob )
In [4]:
def square( t, length ):
for i in range( 4 ):
t.fd( 100, length )
t.lt( 90 )
#square( bob, 100 )
In [5]:
def polygon( t, n, length ):
# Calculate the angle to turn for each side
angle = 360.0 / n
# Draw each side
for i in range( n ):
t.fd( length )
t.lt( angle )
#polygon( bob, 7, 70 )
In [6]:
def polyline( t, length, n, angle ):
"""Draw n line segments with the given length and
angle (in degreees) between them. t is a turtle.
"""
for i in range( n ):
t.fd( length )
t.lt( angle )
help
function
In [7]:
help( polyline )
In [8]:
def draw_square( t ):
# INSERT YOUR CODE HERE
print( 'Remove this line' ) # Jupyter needs a statement to compile
draw_variable_square
that takes a parameter t
, which is a turtle, and a parameter length
, which is the length of a side. The function should use the turtle to draw a square with sides of the specified length.
In [9]:
def draw_variable_square( t, length ):
# INSERT YOUR CODE HERE
print( 'Remove this line' ) # Jupyter needs a statement to compile
draw_polygon
that takes a parameter t
, which is a turtle, a parameter n
, which is the number of sides, and a parameter length
, which is the length of a side. The function should draw a polygon with the specified number of sides of the specified length.
In [10]:
def draw_polygon( t, n, length ):
# INSERT YOUR CODE HERE
print( 'Remove this line' ) # Jupyter needs a statement to compile
draw_circle
that takes a parameter t
, which is a turtle, and a parameter r
, which is a radius. The function should draw a circle with the specified radius.
In [11]:
def draw_circle( t, r ):
# INSERT YOUR CODE HERE
print( 'Remove this line' ) # Jupyter needs a statement to compile