Skip to main content

Repetition Structures

4.1 Introduction to Repetition Structures (Loops)

concept

A repetition structure causes a statement or set of statements to execute repeatedly.

condition-controlled loop, count-controlled loop

4.2 The while Loop: A Condition-Controlled Loop

# Program 4-1 (commission.py)
# This program calculates sales commissions.

# Create a variable to control the loop.
keep_going = 'y'

# Calculate a series of commissions.
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('The commission is $',
format(commission, ',.2f'), sep='')

# See if the user wants to do another one.
keep_going = input('Do you want to calculate another ' +
'commission (Enter y for yes): ')

4.3 The for Loop: A Count-Controlled Loop

4.4 Calculating a Running Total

accumulator,

4.5 Sentinels

4.6 Input Validation Loops

4.7 Nested Loops

4.8 Using break, continue, and else with Loops