generated from mwc/lab_iteration
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:
15
square.py
15
square.py
@@ -1,17 +1,14 @@
|
|||||||
from turtle import *
|
from turtle import *
|
||||||
|
|
||||||
def square(side_length):
|
def square(side_length):
|
||||||
forward(side_length)
|
|
||||||
right(90)
|
sides = [side_length] * 4
|
||||||
forward(side_length)
|
for side in sides:
|
||||||
right(90)
|
forward(side)
|
||||||
forward(side_length)
|
right(90)
|
||||||
right(90)
|
|
||||||
forward(side_length)
|
|
||||||
right(90)
|
|
||||||
|
|
||||||
sizes = [20, 40, 60, 80, 100]
|
sizes = [20, 40, 60, 80, 100]
|
||||||
for size in sizes:
|
for size in sizes:
|
||||||
square(size)
|
square(size)
|
||||||
input()
|
|
||||||
|
|
||||||
|
input()
|
||||||
|
|||||||
Reference in New Issue
Block a user