lenawb
lenawb

Reputation: 47

Using python turtle to draw a polygon with n number of sides

I am new to python with turtle and would appreciate some help. I am trying to create a program that takes an input for a number of sides then draws a regular polygon with that number of sides. However, it either produces a TimeLimitError or it simply draws a straight line.

Here is what I have:

sides = int(input("How many sides would you like? "))
angle = sides / 360

import turtle
for count in range(sides):
  turtle.fd(50)
  turtle.lt(angle)

But this is what it keeps producing:

How many sides would you like?  5
TimeLimitError: Program exceeded run time limit. on line 1

Upvotes: 2

Views: 11978

Answers (2)

O.G
O.G

Reputation: 1

Thank you for the code. I have altered it a little. It was not used as an answer to a test. (It was done on Grok:Blockly playground )I will post below:

from turtle import *


print('I claim no credit to this code.')
print('This was created by lenawb on stackoverflow')
print('https://stackoverflow.com/questions/46714190/using-python- 
turtle-to-draw-a-polygon-with-n-number-of-sides')
Fill_colour = input('Fill colour?' + ' ')
left_or_right_ = input('Left or right?' + ' ')
Side_Length = int(input('Length?' + ' '))
Sides = int(input('Sides?' + ' '))
angle = 360 / Sides
PEN_Size = int(input('Pen size?' + ' '))
pensize(PEN_Size)
fillcolor(Fill_colour)
begin_fill()
if left_or_right_ == 'left':
  for count in range(int(Sides)):
    forward(Side_Length)
    left(angle)
else:
  for count2 in range(int(Sides)):
    forward(Side_Length)
    right(angle)
end_fill()

Upvotes: 0

Payman
Payman

Reputation: 2880

you should divide 360 by number of sides not the other way around.

angle = 360 / sides

Upvotes: 1

Related Questions