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:
We then set all of the numeric variables to zero
We then inform the user what the program does, and ask for input from him/her.
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:
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.
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:
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:
Here is the final program.... enjoy!
|
|