Andrew HB
Andrew HB

Reputation: 412

How to draw a heptagon using Python Turtle with the point at the top?

I am drawing a heptagon using this code:

tegan.setheading(0);

for i in range (7):
    tegan.right(51.43)
    tegan.forward(100)

However, this code always draws the shape with a flat edge at the top and I want the point at the top. What am I doing wrong?

Upvotes: 0

Views: 1861

Answers (2)

cdlane
cdlane

Reputation: 41895

Alternatively, you can simply do:

import turtle

turtle.circle(-116, steps=7)

turtle.done()

enter image description here

Upvotes: 1

mozway
mozway

Reputation: 261860

To make an heptagon, you need to rotate 360°/7 each step. To have the point up, you rotate only half on the first step.

One trick to do this is to rotate in one direction (here left) half the angle, and then proceed in the other direction with the full angle seven times. Another possibility is to write a condition in the loop for the first step, or split the code in two.

import turtle
angle = 360/7
turtle.left(angle/2)
for i in range(7): 
    turtle.right(angle)
    turtle.forward(100)

enter image description here

Upvotes: 2

Related Questions