Reputation: 141
Here is the code. I have tried the following.
Why does this happen? Can anyone explain?
import turtle
turtle.penup()
turtle.setposition(0,300)
turtle.pendown()
turtle.pencolor("red")
turtle.speed(10)
for i in range(6 , 15, 2):
step = int(360 / i)
turtle.setheading(int(90 - (180 - step)/2))
for j in range(1, 1 + i):
turtle.right(step)
turtle.forward(10*i)
turtle.exitonclick()
Is this because of floating number in calculation?
Upvotes: 0
Views: 51
Reputation: 5449
Yes this is because 360 is not nicely divisible by 14. Which is the last iteration of the outer loop.
step
is 360 / 6 => 60step
is 360 / 8 => 45step
is 360 / 10 => 36step
is 360 / 12 => 30step
is 360 / 14 => 25.714285714If you change your code to not truncate the step
value from a float to an integer then it works fine:
for i in range(6, 15, 2):
step = 360.0 / i
turtle.setheading(90 - (180 - step)/2)
for j in range(1, i + 1):
turtle.right(step)
turtle.forward(10*i)
Upvotes: 4