[Tutor] Python Turtle Question
Alan Gauld
alan.gauld at btinternet.com
Fri Sep 12 11:09:08 CEST 2014
On 12/09/14 02:42, Nutthanai Chowwiwat wrote:
> *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.*
>
> This is what I have so far. I am only just starting out. So if possbile I
> want to stick to the basics. I'm pretty sure I need to use a "for loop".
Yes you will want some kind of loop and a for loop is a good candidate.
But first you could try doing some easy examples without a loop - say a
square or triangle - to get the turtle commands working.
> def main():
>
> import turtle
>
> wn = turtle.Screen()
> tess = turtle.Turtle()
>
> #Ask for information to draw
> num_sides = eval(input("How many sides does your shape have?"))
> length_sides = eval(input("How long are the sides?"))
> shape_color = eval(input("What color is your shape"))
You should NOT use eval() like this. It is very insecure and
a very bad habit to fall into. Instead you should convert the
input() calls using explicit conversion functions, like int()
or float() You will need to think about how you want to
convert/store the color since that’s not a native Python
data type.
The turtle module can accept a color name if its a standard
one that it recognises - but what if the user types a
non-standard color? What will your program do then?
But turtle can also accept RGB values instead of a name,
would you prefer them? Its your choice as a programmer
how you do that.
Assuming you now have the values stored you need to start
drawing the shape. I recommend you start the interpreter,
import turtle and start playing with the turtle functions.
Once you understand which functions you want and how to
use them you can return to your program and insert them
as needed.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list