Gettingstarted Answers
Gettingstarted Answers
Self-Review Questions
Self-review 1.2 Why is the following the case – rather than the result being 13?
>>> print "4 + 9"
4+9
>>>
Because "4+9" is a string and so it is printed as is, there is no expression to be
evaluated.
Self-review 1.3 Why does the expression 2 + 3 * 4 result in the value 14 and not
the value 24?
Because the * operator has higher precedence than the operator +. The 3 * 4 subex-
pression is evaluated first giving 12 and then the expression 2 + 12 is evaluated
resulting in 14.
Self-review 1.10 The Turtle module provides a function to draw circles. If this
function was not available, what algorithm would you use to
draw a circle?
Draw a large number of very short straight lines changing the direction of travel
by 360° divided by the number of lines. So if the number is 3 we have a triangle. If
the number is 4 we have a square. If the number is 5 we have a pentagon, with 6 a
hexagon. If we have 100 sides then for a small ‘circle’ it may not be noticeable that
the lines are straight.
turtle.goto = goto(*args)
Go to the given point.
goto(x, y)
go to point (x, y)
Programming Exercises 3
goto((x, y))
go to point (x, y)
Example:
>>> turtle.position()
[0.0, 0.0]
>>> turtle.goto(50, -45)
>>> turtle.position()
[50.0, -45.0]
Programming Exercises
Exercise 1.1 Estimate the size of your computer screen by importing the Turtle
module and causing the turtle to move 1 pixel (though you may want
to try 10 pixels!).
Using the program:
import turtle
turtle.forward ( 1279 )
turtle.right ( 90 )
turtle.forward ( 512 )
import turtle
turtle.forward ( 70 )
turtle.left ( 135 )
turtle.forward ( 50 )
turtle.left ( 90 )
turtle.forward ( 50 )
# Prompt to stop the program from terminating and hence the display from disappearing.
Exercise 1.4 Write a program to draw a red circle, then a yellow circle underneath
it and a green circle underneath that. The result should look some-
thing like:
import turtle
turtle.up ( )
turtle.goto ( 0 , 35 )
turtle.down ( )
turtle.color ( ’red’ )
turtle.circle ( 20 )
turtle.up ( )
turtle.goto ( 0 , -20 )
turtle.down ( )
turtle.color ( ’yellow’ )
turtle.circle ( 20 )
turtle.up ( )
turtle.goto ( 0 , -75 )
turtle.down ( )
turtle.color ( ’green’ )
turtle.circle ( 20 )
Exercise 1.5 Amend your program from the previous question so that the circles
are filled and hence the result looks something like:
Programming Exercises 5
import turtle
turtle.up ( )
turtle.goto ( 0 , 35 )
turtle.down ( )
turtle.color ( ’red’ )
turtle.fill ( 1 )
turtle.circle ( 20 )
turtle.fill ( 0 )
turtle.up ( )
turtle.goto ( 0 , -20 )
turtle.down ( )
turtle.color ( ’yellow’ )
turtle.fill ( 1 )
turtle.circle ( 20 )
turtle.fill ( 0 )
turtle.up ( )
turtle.goto ( 0 , -75 )
turtle.down ( )
turtle.color ( ’green’ )
turtle.fill ( 1 )
turtle.circle ( 20 )
turtle.fill ( 0 )
Exercise 1.6 Write a program to draw a regular hexagon. The result should look
something like:
Hint: Regular hexagons have six sides, of equal length, which meet at
an internal angle of 120°.
6 Getting Started
import turtle
turtle.forward ( 50 )
turtle.right ( 60 )
turtle.forward ( 50 )
turtle.right ( 60 )
turtle.forward ( 50 )
turtle.right ( 60 )
turtle.forward ( 50 )
turtle.right ( 60 )
turtle.forward ( 50 )
turtle.right ( 60 )
turtle.forward ( 50 )
Exercise 1.7 Write a program to draw the first letter of your name.
Challenges
import turtle
turtle.forward ( 10 )
turtle.left ( 120 )
turtle.forward ( 15 )
turtle.left ( 120 )
turtle.forward ( 20 )
turtle.left ( 120 )
turtle.forward ( 25 )
turtle.left ( 120 )
turtle.forward ( 30 )
turtle.left ( 120 )
turtle.forward ( 35 )
turtle.left ( 120 )
turtle.forward ( 40 )
turtle.left ( 120 )
turtle.forward ( 45 )
Challenges 7
turtle.left ( 120 )
turtle.forward ( 50 )
turtle.left ( 120 )
turtle.forward ( 55 )
turtle.left ( 120 )
turtle.forward ( 60 )
turtle.left ( 120 )
turtle.forward ( 65 )
turtle.left ( 120 )
turtle.forward ( 70 )
turtle.left ( 120 )
turtle.forward ( 75 )
turtle.left ( 120 )
turtle.forward ( 80 )
turtle.left ( 120 )
turtle.forward ( 85 )
turtle.left ( 120 )
turtle.forward ( 90 )
turtle.left ( 120 )
turtle.forward ( 95 )
turtle.left ( 120 )
Hint: You may want to start by doing some trigonometry to sort out
all the angles and lengths. Pythagoras’ Theorem will almost
certainly come in useful: Pythagoras’ Theorem states that for a
right-angled triangle:
p
hypotenuse = x2 + y2
#! /usr/bin/env python
import turtle
import math
8 Getting Started
# Assume the house body is a square and the roof is a right-angled triangle (half a square).
We
# can then get the various diagonals using Pythogoras’ Theorem with all angles being multiples
# of 45.
# It seems almost totally unreasonable not to use variables to solve this problem.
sideLength = 40
halfDiagonalLength = 0.5 * math.sqrt ( 2 * ( sideLength ** 2 ) )
turtle.left ( 90 )
turtle.forward ( sideLength )
turtle.right ( 135 )
turtle.forward ( halfDiagonalLength )
turtle.right ( 90 )
turtle.forward ( halfDiagonalLength )
turtle.left ( 135 )
turtle.forward ( sideLength )
turtle.left ( 135 )
turtle.forward ( halfDiagonalLength )
turtle.right ( 90 )
turtle.forward ( halfDiagonalLength )
turtle.left ( 135 )
turtle.forward ( sideLength )
turtle.right ( 135 )
turtle.forward ( halfDiagonalLength )
turtle.right ( 90 )
turtle.forward ( halfDiagonalLength )
turtle.right ( 45 )
turtle.forward ( sideLength )
# Prompt to stop the program from terminating and hence the display from disappearing.