Question
Regular Polygons with Turtle Graphics: Python Create a Python program that will draw, using Turtle graphics, a regular polygon, one with each side the same
Regular Polygons with Turtle Graphics: Python
Create a Python program that will draw, using Turtle graphics, a regular polygon, one with each side the same length and each angle of the same size, with the number of sides greater than 2 as chosen by the user.
Your code should prompt the user to enter the number of sides he/she would like to see. Use a while loop to allow the user to draw as many polygons as desired. Use the number of sides being less than 3 as the indicator that the user does not want to draw any more polygons. Use a for loop to draw the polygon with side length of (600umber of sides) pixels, that gives us a polygon of circumference of 600 pixels, and a color from the colors list as specified below. The angle, in degrees, to turn right or left may be calculated as 360 divided by the number of sides. For example, the angle for a 12 sided polygon is 360/12 = 30. The angle for a six sided polygon is 60 degrees. The polygons will be drawn with a border color, a fill color, and a border width. Before each new polygon is drawn, the turtle graphic area should be cleared. Set the shape of the turtle to look like a turtle.
The colors list should be defined as follows:
# Specify the colors list to choose the line color and fill color. colors = ['coral', 'gold', 'brown', 'red', 'green', 'blue', 'yellow', 'purple', 'orange', 'cyan', 'pink', 'magenta', 'goldenrod']
To select a color from the colors list, generate a random integer in the range 0-12 and use that as the index into the colors list.
To make sure that the border does not get too fat, we will set its size to be (sides%20) + 1. The modulus operator(%) will give us a value of 0-19, that will give us a width of 1-20.
To draw the polygon, you will write a function with the following header definition:
# The makePolygon function draws a polygon with the number of sides, # side length, border color, border width, and fill color as specified. def makePolygon (sides, length, borderColor, width, fillColor):
where sides is the number of sides of the polygon, length is the length of each side, borderColor is the color of the border of the polygon, width is the width of the border, and fillColor is color the polygon is filled with. This function will be called from the main part of your program to draw the polygons.
Sample execution: Python 3.64 Shell File Edit Shell Debug Options Window Help Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MPython Turtle Graphics Type "copyright", "credits" or "license )" for more inf RESTART: P:/Cmpsc131/Programming Assignment Solutions/ This program will draw a polygon with 3 or more sides. Enter the number of sides, less than 3 to exit: 7 Enter the number of sides, less than 3 to exit: 19 Enter the number of sides, less than 3 to exit: 20 Enter the number of sides, less than 3 to exit: 18 Enter the number of sides, less than 3 to exit: 18 Enter the number of sides, less than 3 to exit: 100 Enter the number of sides, less than 3 to exit: 100 Enter the number of sides, less than 3 to exit: 100 Enter the number of sides, less than 3 to exit: 120 Enter the number of sides, less than 3 to exit: 119 Enter the number of sides, less than 3 to exit: 4 Enter the number of sides, less than 3 to exit: 4 Enter the number of sides, less than 3 to exit: 6 Enter the number of sides, less than 3 to exit: 1 Thanks for using the polygon generator program
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started