CP104 - Repetition Structures - Chapter 4
A repetition structure causes a statement or set of statements to execute repeatedly.
The while Loop: A Condition-Controlled Loop
# This program calculates sales commissions.
# Create a variable to control the loop.
keep_going = 'y'
# Calculate a series of commissions.
while keep_going == 'y':
sales = float(input('Enter the amount of sales: '))
comm_rate = float(input('Enter the commission rate: '))
# Calculate the commission.
commission = sales * comm_rate
# Display the commission.
print(f'The commission is ${commission:,.2f}.')
# See if the user wants to do another one.
keep_going = input('Do you want to calculate another ' + ‘'commission (Enter y for
yes): ')
INFINITE LOOPS
This program demonstrates an infinite loop. # Create a variable to control the loop. keep_going
= 'y'
# Warning! Infinite loop!
while keep_going == 'y':
# Get a salesperson's sales and commission rate.
sales = float(input('Enter the amount of sales: ')) comm_rate = float(input('Enter the
commission rate: '))
# Calculate the commission.
commission = sales * comm_rate
# Display the commission.
print(f'The commission is ${commission:,.2f}.')
The for Loop: A Count-Controlled Loop
for variable in [value1, value2, etc.]:
statement
statement
etc.
#This program demonstrates a simple for loop that uses a list of numbers.
print('I will display the numbers 1 through 5.')
for num in [1, 2, 3, 4, 5]:
print(num)
Program Output
I will display the numbers 1 through 5.
1
2
for loop that uses the range function:
for num in range(5):
print(num)
Sentinels
A sentinel is a special value that marks the end of a sequence of values.
Input Validation
Input validation is the process of inspecting data that has been input to
a program, to make sure it is valid before it is used in a computation. Input validation is
commonly done with a loop that iterates as long as an input variable references bad data.
RETAIL PRICES
# This program calculates retail prices.
MARK_UP = 2.5 # The markup percentage
another = 'y' # Variable to control the loop.
# Process one or more items.
while another == 'y' or another == 'Y':
# Get the item's wholesale cost.
wholesale = float(input("Enter the item's wholesale cost: "))
Calculate the retail price.
retail = wholesale * MARK_UP
# Display the retail price.
print(f'Retail price: ${retail:,.2f}')
# Do this again?
another = input('Do you have another item? ' +
'(Enter y for yes): ')
Nested Loops
A loop that is inside another loop is called a nested loop.
TEST SCORES
# This program averages test scores. It asks the user for the number of students and the
number of test scores per student.
# Get the number of students.
num_students = int(input('How many students do you have? '))
# Get the number of test scores per student.
num_test_scores = int(input('How many test scores per student? '))
# Determine each student's average test score.
for student in range(num_students):
# Initialize an accumulator for the test scores.
total = 0.0
# Display the student number.
print(f'Student number {student + 1}')
print('-----------------')
# Get the student's test scores.
for test_num in range(num_test_scores)
print(f'Test number {test_num + 1}', end='')
score = float(input(': '))
# Add the score to the accumulator. total += score
# Calculate the average test score for this student.
average = total / num_test_scores
# Display the average.
print(f'The average for student number {student + 1} ‘
f'is: {average:.1f}') print()