How to Draw an n-sided Polygon in Python Using the Turtle Library

# import turtle library
import turtle
# get screen
Window = turtle.Screen()
# name turtle
bob = turtle.Turtle()

# prompt user for num of sides they want their polygon to be
sidesInput = int(input("Enter a number greater than or equal to 3: "))  

# if the user inputed a number less than 3 for the number of sides they want in their polygon, print ERROR because a polygon cannot have less than 3 sides
if sidesInput < 3 :
    print('ERROR')

# initialize variables    
i = 0
forward = ''
left = ''

# using a while-loop
while i in range (sidesInput):
# make forward variable equal to 100
    forward = 100
# make left variable 360 degrees divided by the number of sides inputed by user to get the angle at which to rotate to make the figure
    left = 360/sidesInput
# while condition- make i = i + 1 
    i += 1
  
# turtle 
# make bob go forward by forward variable 
    bob.forward(forward)
# make bob go left by left variable 
    bob.left(left)
Design a site like this with WordPress.com
Get started