Python Chapter5
Python Chapter5
Loops
Introduction
A loop can be used to tell a program to execute statements
repeatedly.
Suppose that you need to display a string (e.g., programming is
fun!) 100 times.
count = 0
while count < 100:
print("Programming is fun!")
count = count + 1
while True:
Statements
while loop-continuation-condition:
Statements
Additional statements for controlling the loop
Multiple Subtraction Quiz
SubtractionQuiz.py, generates just one question for each
run. How do you write the code to generate five questions?
The program that generates five questions and, after a
student answers all of them, reports the number of correct
answers. The program also displays the time spent on the
test, as shown in the sample run.
What is 2 - 0? 2
You are correct!
What is 9 - 6? 3
You are correct!
What is 7 - 0? 7
You are correct!
What is 2 - 0? 2
You are correct!
What is 4 - 2? 2
You are correct!
Correct count is 5 out of 5
Test time is 15 seconds
Controlling a Loop with User
Confirmation
If you want the user to decide whether to take another
question, you can offer a user confirmation. The template of
the program can be coded as follows:
continueLoop = 'Y'
while continueLoop == ‘Y’:
# Execute the loop body once
...
# Prompt the user for confirmation
continueLoop = input("Enter Y to continue and N to
quit: ")
Controlling a Loop with a Sentinel
Value
Another common technique for controlling a loop is to
designate a special input value, known as a sentinel value,
which signifies the end of the input. A loop that uses a
sentinel value in this way is called a sentinel-controlled loop.
count = 5 count = 5
while count < n: while count < n:
count += 1 count = count + 3
Nested Loops
A loop can be nested inside another loop.
Nested loops consist of an outer loop and one or more inner
loops. Each time the outer loop is repeated, the inner loops
are reentered and started anew.
Ex: write a program that uses nested for loops to display a
multiplication table.
MultiplicationTable.py
print(" Multiplication Table")
# Display the number title
print(" |", end = '')
for j in range(1, 10):
print(" ", j, end = '')
print() # Jump to the new line
print("——————————————————————————————————————————")
The number is 14
The sum is 105
Keywords break and continue
You can also use the continue keyword in a loop. When it is
encountered, it ends the current iteration and program control
goes to the end of the loop body
sum = 0
number = 0
while number < 20:
number += 1
if number == 10 or number == 11:
continue
sum += number
>>>
The first 50 prime numbers are
2 3 5 7 11 13 17 19 23 29
31 37 41 43 47 53 59 61 67 71
73 79 83 89 97 101 103 107 109 113
127 131 137 139 149 151 157 163 167 173
179 181 191 193 197 199 211 223 227 229
PROGRAMMING EXERCISES
Q1: Write a program that prompts the user to enter the
number of students and each student’s score, and displays
the highest score. Assume that the input is stored in a file
named score.txt, and the program obtains the input from the
file.
Q2: Use nested loops that display the following patterns in
four separate programs:
PROGRAMMING EXERCISES
Q3: Write a program that lets the user enter the loan amount
and loan period in number of years and displays the monthly
and total payments for each interest rate starting from 5% to
8%, with an increment of 1/8. Here is a sample run:
M = P * ( J / (1 - (1 + J)-N)). Follow the steps below for a
detailed guide to using this formula, or refer to this quick
explanation of each variable:
M = payment amount
P = principal, meaning the amount of money borrowed
J = effective interest rate. Note that this is usually not the
annual interest rate; see below for an explanation.
N = total number of payments
http://www.hughcalc.org/formula.php