generated from mwc/lab_iteration
	square.py
I put documentation in my code, explaining each line. I hadn't written code in 12 months, so I had to refresh my previous learning. You have to think logically, unlike computers, which don't think at all! For larger programs, draw a flowchart!
This commit is contained in:
		
							
								
								
									
										30
									
								
								square.py
									
									
									
									
									
								
							
							
						
						
									
										30
									
								
								square.py
									
									
									
									
									
								
							@@ -1,17 +1,21 @@
 | 
			
		||||
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)
 | 
			
		||||
    right(90)
 | 
			
		||||
 | 
			
		||||
sizes = [20, 40, 60, 80, 100]
 | 
			
		||||
for size in sizes:
 | 
			
		||||
    square(size)
 | 
			
		||||
input()
 | 
			
		||||
sizes = [20, 40, 60, 80, 100] # list
 | 
			
		||||
for size in sizes: # outer loop, different square sizes from list
 | 
			
		||||
    def square(): # function definition
 | 
			
		||||
        for x in range(0,4): # inner loop creating a square
 | 
			
		||||
            forward(size) # size doesn't change until the outer loop is executed
 | 
			
		||||
            right(90) # right turn 90 degrees
 | 
			
		||||
    
 | 
			
		||||
    # this code block for the outer loop (below) has to be 
 | 
			
		||||
    # precisely indented in line with the def square(): function definition,
 | 
			
		||||
    # otherwise it won't run properly
 | 
			
		||||
    pendown() # same starting point for each square
 | 
			
		||||
    color('blue')
 | 
			
		||||
    square() # first calling and drawing of a square side from the inner loop,
 | 
			
		||||
    # then drawing a different square size from the "for loop" outer loop
 | 
			
		||||
    penup()
 | 
			
		||||
 | 
			
		||||
input() # pause the turtle display screen to see the drawing
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user