Python Forum

Full Version: turtle polygon as specified by user input
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
The following is an exercise question from tutorial course and my code.

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.

import turtle      
wn = turtle.Screen()  
alex = turtle.Turtle()

sides = input ("Number of sides in polygon?"  )
length = input ("Length of the sides in polygon?" )
colorname = input ("Color of polygon?" )
fcolor = input ("Fill color of polygon?")

alex.color = (colorname)
alex.fillcolor = (fcolor)

for i in range(sides):
   alex.forward (length)
   alex.left (360 / sides)
Input pop-ups work, but otherwise, no output.
Check for error messages.
If you are running this in Python 3.0+ (which you should be), all those inputs will return strings, but range, forward, and division are expecting numbers. You need to convert the string to numbers with int() or float() as appropriate.
Thanks for the tip. Reading the error and a little trial and error and I was able to get turtle to draw polygon.
I'm still having trouble with color and fillcolor of polygon. Any suggestions?
import turtle      
wn = turtle.Screen()  
alex = turtle.Turtle()

sides = input ("Number of sides in polygon?"  )
length = input ("Length of the sides in polygon?" )
colorname = input ("Color of polygon?" )
fcolor = input ("Fill color of polygon?")

alex.color = (colorname)
alex.fillcolor = (fcolor)

for i in range(int(sides)):
   alex.forward (int(length))
   alex.left (int(360)/int(sides))
Are you getting errors for the color and fillcolor? If so, please post them.
No error messages!
color and fillcolor are both functions, not values you assign, so you need to remove the = on those lines. Also, color sets both the pencolor and the fillcolor, I would use pencolor there instead for clarity. Finally, filling is only done if you start the figure with alex.begin_fill() and end it with alex.end_fill()
thanks ichabod801, it did the trick