Wouldn't it be great to have a console log that shows what happens insider your program.
Debugging
We could use simple print
statements for that.
As a benefit we have debugging for free.
side = 3
angle = (360 / side)
print(f"angle: {angle}, side: {side}") # initial shape
while True:
for _ in range(side):
print(f"angle: {angle}, side: {side}") # you will see only side changes
timmy.fd(100)
timmy.right(angle)
side += 1
Refactoring: Make angle a function of side
This way you can only calculate the angle for a given side input:
def angle(side):
return 360 / side
What happens if side = 0
? Maybe add a guard-statement to prevent ZeroDivisionError
.
Fix
from turtle import Turtle
timmy = Turtle()
timmy.shape("turtle")
timmy.color("green")
def angle(side):
if side == 0:
return 0
return 360 / side
side_count = 3 # start with a triangle
while True:
inner_angle = angle(side_count)
print(f"next shape >> sides: {side_count}, inner angle: {inner_angle}") # see both changed
for _ in range(side_count):
timmy.fd(100)
timmy.right(inner_angle)
side_count += 1
So far your loop will be infinite ... because of while True
it never ends.
How many rounds should it loop? What is the upper bound for side_count
?
You could set a limit like:
while side_count < 10:
Improvement
Keep loops short, express the purpose of draw a shape with a function:
# default for parameter sides is 3
def draw_shape(sides = 3):
if sides < 3:
return # at least 3 sides required to draw a shape
inner_angle = angle(sides)
print(f"next shape >> sides: {sides}, inner angle: {inner_angle}") # see both changed
for _ in range(sides):
timmy.fd(100)
timmy.right(inner_angle)
side_count = 3 # start with a triangle
while side_count < 25:
draw_shape(side_count)
side_count += 1