04 Repetition Structures
04 Repetition Structures
4. Repetition Structures
Topics
Introduction to Repetition Structures
The while Loop: a Condition-Controlled Loop
The for Loop: a Count-Controlled Loop
Calculating a Running Total
Sentinels
Input Validation Loops
Nested Loops
Flow Control
Turtle Graphics: Using Loops to Draw Designs
doubled_number = doubled_number * 2
print(doubled_number)
doubled_number = doubled_number * 2
print(doubled_number)
doubled_number = doubled_number * 2
print(doubled_number)
doubled_number = doubled_number * 2
print(doubled_number)
doubled_number = doubled_number * 2
print(doubled_number)
doubled_number = doubled_number * 2
print(doubled_number)
Syntax
while condition:
statement
statement
etc.
Syntax
for variable in iterable
statement
statement
etc.
Target variable: the variable which is the target of the assignment at the beginning of each iteration
The usual variable naming rules apply
for num in [1, 2, 3, 4, 5]:
print(num)
Using the range Function with the for Loop
for num in [0, 1, 2, 3, 4]:
print(num)
The range function can be used to generate a sequence with numbers in descending order
Make sure starting number is larger than end limit, and step value is negative
for num in range(10, 1, -1):
print(num)
Checkpoint
1. Rewrite the following code so it calls the range function instead of using the list [0, 1, 2, 3, 4, 5].
for x in [0, 1, 2, 3, 4, 5]:
print(‘I love to program!')
keep_going = 'y'
Computer cannot tell the difference between good data and bad data
If user provides bad input, program will produce bad output
GIGO: garbage in, garbage out
It is important to design program such that bad input is never accepted
Input validation: inspecting input before it is processed by the program
If input is invalid, prompt user to enter correct data
Commonly accomplished using a while loop which repeats as long as the input is bad
If input is bad, display error message and receive another set of data
If input is good, continue to process the input
# Do this again?
another = input('Do you have another item? (Enter y for yes): ')
# Do this again?
another = input('Do you have another item? (Enter y for yes): ')
Checkpoint
1. What does the phrase "garbage in, garbage out" mean?
2. Give a general description of the input validation process.
3. Describe the steps that are generally taken when an input validation loop is used to validate data.
Nested Loops
Nested loop: loop that is contained inside another loop
Key points about nested loops:
Inner loop goes through all of its iterations for each iteration of outer loop
Inner loops complete their iterations faster than outer loops
Total number of iterations in nested loop:
number_iterations_inner x number_iterations_outer
Example: Clock
Analog clock works like a nested loop
Hours hand moves once for every 60 movements of the minutes hand: for each iteration of the “hours,” do 60
iterations of “minutes”
Seconds hand moves 60 times for each movement of the minutes hand: for each iteration of “minutes,” do 60
iterations of “seconds”
# We use time module to sleep for 1 second
import time
for minutes in range(60):
for seconds in range(60):
print(f'{minutes:02d}:{seconds:02d}')
# Wait 1 second
time.sleep(1)
# Wait 1 second
time.sleep(1)
for r in range(rows):
for c in range(cols):
print('*', end='')
print()
turtle_square.py
turtle_octagon.py
turtle_circles.py
turtle_starburst.py
# Draw a square sized 100px
import turtle
for x in range(4):
turtle.forward(100)
turtle.right(90)
turtle.hideturtle()
turtle.done()
# Draw an octogon sized 100px
import turtle
import math
for x in range(8):
turtle.forward(100)
turtle.right(45)
turtle.hideturtle()
turtle.done()
turtle.speed(0)
for x in range(NUM_CIRCLES):
turtle.circle(RADIUS)
turtle.left(ANGLE)
turtle.hideturtle()
turtle.done()
turtle.hideturtle()
turtle.done()
Summary
This chapter covered:
Repetition structures, including:
Condition-controlled loops
Count-controlled loops
Nested loops
Infinite loops and how they can be avoided
range function as used in for loops
Calculating a running total and augmented assignment operators
Use of sentinels to terminate loops
Use break, continue and else to control flow of a loop
Using loops to draw turtle graphic designs