0

I want to preface this by saying I'm very much a beginner. From my understanding, the side variable should increment by one indefinitely under the while loop and therefore the angle variable will reflect the new equation. Using a code visualizer, I can see that the side variable is updating, but the angle variable continues to produce a float of 120.0. I've tried different indentations of the last line, but I'm getting the same result.

What should be happening is each loop will create a shape with one more side, but I'm only getting a triangle.

from turtle import Turtle

timmy = Turtle()

timmy.shape("turtle")
timmy.color("green")

side = 3
angle = (360 / side)

while True:
    for _ in range(side):
        timmy.fd(100)
        timmy.right(angle)

    side += 1
andrew
  • 81
  • 1
  • 8
  • 1
    You aren't updating ```angle``` each time you change ```side```. You should put ```angle = 360/side``` under ```side += 1``` – Nin17 Jul 18 '22 at 17:35
  • @Nin17 Thanks! I originally tried that by removing the first angle reference which caused the for loop to see it as undefined, but writing it twice works. Is there a more efficient way to write it rather than having it written twice? – andrew Jul 18 '22 at 17:40
  • 2
    you could do ```def angle(side): return 360/side``` and then call ```angle(side)``` in ```timmy.right()``` – Nin17 Jul 18 '22 at 17:41
  • Primitive variables don't update by reference. They don't represent depenndency chains--you might be thinking in terms of declarative or reactive programming. You have to update `angle` manually at the program point where you need it to have a specific value. – ggorlen Jul 18 '22 at 19:24

1 Answers1

0

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
hc_dev
  • 8,389
  • 1
  • 26
  • 38