In: Computer Science
Write a program (polygon.py) that asks the user to enter the number of sides in a regular polygon. For example, an equilateral triangle is a regular 3-sided polygon, a square is a regular 4-sided polygon, and a pentagon is a regular 5-sided polygon. If a user enters a number of sides between 3 and 25, inclusive, draw the polygon and wait for the user to click on the screen to quit the program. If the user enters a number less than 3 or greater than 25, tell them the number of sides must be between 3 and 25, and quit the program
Python.
We can use the turtle module in python to draw the polygon.
Any exterior angle of a polygon is where n is the number of sides in the polygon. So the internal angle will be .
by internal and external angle we mean.
Here is the code to do so:
The output is:
if the answer helped please upvote. and for any doubts ask in the comments.
Code:
import turtle # import the turtle module to draw the
polygon
# take the number of sides as input
numberofSides = int(input('Enter the number of sides in the
polygon:'))
# if the number of sides is less then 3 or
# greater than 25 then quit the script
if numberofSides<3 or numberofSides>25:
print("the number of sides must be between 3 and 25")
quit()
# calculate the external angle
externalAngle = (360/numberofSides)
print(externalAngle)
t = turtle.Turtle()
# for each side draw a straight line and
# then turn the turtle by external angle
for i in range(numberofSides):
t.forward(100) #Assuming the side of a pentagon is 100 units
t.right(externalAngle) #Turning the turtle by the external angle
degree
turtle.done()