Custom Search

PYTHON: Turtle Graphics

- Drawing an equilateral figure

This program enables the user to select the number of sides that the turtle draws.

 

We start off by importing the turtle module into our program:

import turtle

We then set all of the numeric variables to zero

count = 0

n = 0

a = 0

We then inform the user what the program does, and ask for input from him/her.

print("This turtle graphic program draws an equilateral figure for you.")

print("You decide on the number of sides you wish the turtle cursor to draw on your screen.")

print("")

print("")

The user is supposed to enter a number - but users are unpredictable, and sometimes just careless - so the programmer has to incorporate 'guiding' elements in the code to ensure the user does what is expected within the parameters of the program. Otherwise the program will crash.

A 'while loop' set to repeatedly ask until the user enters a number allows us to stop any other characters that integer numbers being given as a parameter:

while True:

try:

n=int(input("Please choose the number of sides you want the figure to have:"))

break # this section asks for an integer input

except ValueError: #this section comes into play if they enter something that gives a 'value error' - in other words enter anything other than an integer.

print("")

print("")

print("You can only enter whole numbers - not decimals, letters or symbols.")

print("") #the print("")s scattered around simply space out the computer responses, making it easier to read for the user...

Now we have to guide the user to choose a number in a range that will be sensible for this program.

The user could put in any value for number of sides - we have only, so far, guided the user to enter an integer value.

If the number is too small (or a negative number) an enclosed figure cannot be drawn, if it is too large the computer could end up spending a long time simply drawing a circle as the stright lines would reduce to being just a pixel!

We therefore use a 'while/if/else' command to ensure the number chosen is sensible.

while n<3 or n>25:

if n<3:

print("To draw an enclosed figure there must be at least 3 sides...")

if n>25:

print("The number of sides you have chosen is so great that each side would only be virtually a pixel in size and the figure would look like a circle.")

n=int(input("Please choose the number of sides you want the figure to have:"))

else:

a = 360/n # the angle the turtle must turn through is the full circle (360o) divided by the number of sides it must draw.

We now have to set up the screen.

I have designed a graphic background for this program. I have also set the cursor to be a Pink Fuzzle. Both of these images have to be imported from my Trinket library - it takes png and gif images.

You can see how I incorporate the graphics below:

screen=turtle.Screen()

screen.setup(550,450)

screen.bgpic("background.png")# set the screen background

screen.addshape("FuzzlePINK.png")#Setting the cursor to be a Fuzzle I first add the shape

fuzzle = turtle.Turtle("FuzzlePINK.png")#Then I set it as a cursor

fuzzle.shape("FuzzlePINK.png")

The movement of the Fuzzle needs repeating for the number selected by the user.

We achieve this using 'while/else' and counting the number of times the motion is repeated:

fuzzle.down()

while count<n:

fuzzle.forward(123*5/n)

fuzzle.rt(a)

count=count+1

else: fuzzle.rt(270)


Here is the final program.... enjoy!