I replaced the four repeated forward and right command with a for-loop that iterates over a list of sides, so each command is written only once while still drawing the same square

Checkpoint 1:

I found it difficult to rewrite square() using a for-loop. It has been a long time since I took a coding course so I did not remember much from what I did in previous coding courses to apply here.
I did a lot of trial and error to see what I remembered about for-loops and if all went wrong I looked online for examples of how a for-loop properly looks.
This commit is contained in:
angelotr
2025-09-13 21:29:39 -04:00
parent 2be7ebf5c5
commit 43d3315af6

View File

@@ -1,17 +1,14 @@
from turtle import *
def square(side_length):
forward(side_length)
right(90)
forward(side_length)
right(90)
forward(side_length)
right(90)
forward(side_length)
sides = [side_length] * 4
for side in sides:
forward(side)
right(90)
sizes = [20, 40, 60, 80, 100]
for size in sizes:
square(size)
input()
input()