Python Forum
Turtle Polygon drawing program
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Turtle Polygon drawing program
#1
Hello everyone, my name's Tim and I'm a teacher and guess what: I've run out of coding skills Huh

I'm going through the thinkcspy learning program to get a good grip on the foundations of python before I do really funky stuff with networks and robots and stuff.

I'm writing a polygon drawing program with the turtle module. Here's the code:
sides_choice = input("how many sides?")
sides = int(sides_choice)
print (sides)
length_choice = input("how long are the sides?")
length = int(length_choice)
print (length)
lines = input("what colour lines?")
print (lines)
fill = input("what colour fill?")
print (fill)

import turtle
wn = turtle.Screen()
bob = turtle.Turtle()

bob.color(lines)
bob.fillcolor(fill)
for polygon in range(sides):
    bob.fd(length)
    bob.left(((sides -2)*180)/sides)
My problem is with the last line. I'm pretty confident of the maths being correct because the formula for the total interior angles for a regular polygon is (n-2)*180 where 'n' is the number of sides. And then to get each individual corner angle you then divide the result of the last formula with n again.

But I'm not getting the result I should. I'm wondering if there's a syntax error with how I've laid out the arithmetic??
Any help is appreciated Smile
Reply
#2
The error is how you are thinking of the turtle. As the turtle is going around drawing the figure, the turns it is making are not the interior angles, they are the complements of the interior angles. Think about walking around the outside of the Pentagon in DC. When you turn around the corner, what is the angle you turn?

Also note that you need to use begin_fill() before moving and end_fill() after moving to actually fill the polygon.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Ahhh! that makes sense.
Thank you ichabod801!
Reply
#4
Oh, and I meant to say that the solution is to use 180 - ((n-2) * 180) / n as the angle. But perhaps that was obvious.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Create a turtle drawing from .txt file Noob101 20 8,899 Jan-29-2021, 04:13 PM
Last Post: nilamo
  Drawing wheels in a turtle woehh 1 1,882 Apr-23-2020, 02:58 PM
Last Post: deanhystad
  Divide a Python Turtle program into subprograms TurtleOHG 5 3,394 Mar-21-2020, 02:07 PM
Last Post: buran
  turtle polygon as specified by user input johneven 7 10,734 Mar-02-2019, 10:11 PM
Last Post: johneven
  How to hold a program in the turtle (Tic-Tac-Toe game assignment) kmchap 1 4,558 May-17-2018, 05:39 PM
Last Post: j.crater
  Turtle drawing Right Triangle Zatoichi 3 5,713 Feb-26-2018, 12:24 AM
Last Post: Zatoichi
  Help drawing circles with turtle songminguk 3 4,476 Dec-19-2017, 08:43 PM
Last Post: squenson

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020