Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Regular Polygons with Turtle Graphics Python Regular Polygons with Turtle Graphics Create a Python program that will draw, using Turtle graphics, a regular polygon, one

Regular Polygons with Turtle Graphics Python

Regular Polygons with Turtle Graphics

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.

Full documentation about all the functions that can be used with turtle can be found at: https://docs.python.org/3.3/library/turtle.html?highlight=turtle There are several functions to do the same thing in turtle, you may pick any one that works. Your program will need to use turtle functions such as clear(), pencolor(), fillcolor(), shape(), pen_size(), begin_fill(), end_fill(), or ones that do similar things.

Your polygon will not necessarily have the same colors since they are randomly selected. Notice the turtle in the drawing, its' back is the color of the fill.image text in transcribed

Sample execution: Python 3.6.4 Shell File Edit Shell Debug Options 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 Window Help 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. Your polygon will not necessarily have the same colors since they are randomly selected. Notice the turtle in the drawing, its' back is the color of the fill Sample execution: Python 3.6.4 Shell File Edit Shell Debug Options 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 Window Help 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. Your polygon will not necessarily have the same colors since they are randomly selected. Notice the turtle in the drawing, its' back is the color of the fill

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Marketing The Ultimate Marketing Tool

Authors: Edward L. Nash

1st Edition

0070460639, 978-0070460638

More Books

Students also viewed these Databases questions

Question

a. Do team members trust each other?

Answered: 1 week ago