Python Forum
turtle polygon as specified by user input
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
turtle polygon as specified by user input
#1
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.
Reply
#2
Check for error messages.
Reply
#3
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
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))
Reply
#5
Are you getting errors for the color and fillcolor? If so, please post them.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
No error messages!
Reply
#7
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()
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#8
thanks ichabod801, it did the trick
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Simulate an answer based on user input [Beginner needs guidance] Bombardini 1 1,257 Nov-12-2022, 03:47 AM
Last Post: deanhystad
  Print user input into triangle djtjhokie 1 2,343 Nov-07-2020, 07:01 PM
Last Post: buran
  Changing Directory based on user input paulmerton4pope 13 7,886 Aug-14-2020, 11:48 AM
Last Post: GOTO10
  how to add the user input from file into list wilson20 8 4,230 May-03-2020, 10:52 PM
Last Post: Larz60+
  Writing a function that changes its answer based on user input SirRavenclaw 2 2,761 Dec-21-2019, 09:46 PM
Last Post: Clunk_Head
  Print the longest str from user input edwdas 5 4,052 Nov-04-2019, 02:02 PM
Last Post: perfringo
  how to add user input to a dictionary to a graph KINGLEBRON 3 2,981 Jul-31-2019, 09:09 PM
Last Post: SheeppOSU
  Turtle Polygon drawing program tp_oz 3 3,107 Jul-23-2019, 01:01 PM
Last Post: ichabod801
  New to Python - tiny coding assistance on user input function and assign to variable Mountain_Duck 1 2,465 Mar-23-2019, 06:54 PM
Last Post: Yoriz
  Extracting list element with user input valve 1 2,534 Mar-11-2019, 07:37 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020