Repetition
Control
Structure
CHAPTER 4
Repetition
A repetition statement (loops) is used to repeat a group(block) of
programming instructions. In Python, we generally have two
loops/repetitive statements:
• for loop
• while loop
for loop
A for loop is used to iterate over a sequence that is either a range, list, tuple,
dictionary, or a set. We can execute a set of statements once for each item in a
range, list, tuple, or dictionary.
for loop with else Here, the for loop
prints items of the
A for loop can have an optional else list until the loop
block as well. The else part is exhausts.
executed if the items in the
sequence used in for loop exhausts.
Here is an example to illustrate this.
When the for loop
exhausts, it
executes the block
of code in the else
and prints No items
left.
while loop
In Python, while loops are used to execute a block of
statements repeatedly until a given condition is satisfied.
Then, the expression is checked again and, if it is still true,
the body is executed again. This continues until the
expression becomes false.
We generally use this loop when we don't know the
number of times to iterate beforehand.
while loop
In Python, the body of the while loop is
determined through indentation.
The body starts with indentation and the first
unindented line marks the end.
While loop with else Here, we use a counter
variable to print the
Same as with for loops, while loops can string Inside loop three
also have an optional else block. times.
The else part is executed if the On the fourth iteration,
condition in the while loop evaluates to the condition in while
False. becomes False. Hence,
the else part is
Example to illustrate the use of else executed.
statement with the while loop:
Loop example
• Write Python code that prints the word Hello three times using a for loop.
Example:
8
Loop exercise 1
• Write some code that prints
the following workout
instructions using for loop
and while loop:
Ten planks
Five star jumps
Sample of output:
9
Loop exercise 2
• Write some code that asks the
user how many slices of pizza they
want.
• Your program must print the word
PIZZA the required number of
times.
Sample of output:
10
Loop exercise 3
Rocket Launcher
• Write some code that counts down
from 5 to 1, and then prints the
words Blast Off!
Sample of output:
11
Loop exercise 4 12
Write a program that takes marks of 10 students as input. It
calculates the class average and displays it on the screen.
Interaction with the program might look like this. Use while loop.
Welcome
Enter marks: 10
Enter marks: 20
Enter marks: 22.5
Enter marks: 70
Enter marks: 54.75
Enter marks: 87.5
Enter marks: 90.5
Enter marks: 43
Enter marks: 24
Enter marks: 45
Class Average is: 46.725
For Loop Answer suggestion
13
While Loop Answer suggestion
14
Loop exercise 5
Write a Python program that will read a series of
numbers repeatedly until the user key-in number 0
(zero). Then, the program will calculate and display the
total and average of the numbers. Below is the sample
of output for the program.
Sample of output
Please insert a series of number to be
calculated.
Enter a number: 200
Enter a number: 100
Enter a number: 20
Enter a number: 0
Total : 320
Average : 106.67
15
While Loop Answer suggestion
16
While Loop Exercise 6
Type of menu Price (RM)
Che Yah delight 1 20.00
1. Read a cashier name.
2. Read number of orders.
3. Based on number or orders, calculate the total price.
4. The process will be repeated if user enters “Y” to
continue.
5. If user click “N” to stop using the system, display
“Thank You”.
17
Answer’s suggestion
18
While Loop Exercise 7
Type of menu Price (RM)
Che Yah delight 1 20.00
1. Read a cashier name.
2. Read number of orders.
3. Based on number or orders, calculate the total price.
4. The process will be repeated if user enters “Y” to
continue.
5. If user click “N” to stop using the system, display the
total number of transactions and total sales for the
day.
19
While loop exercise 7 solution
20
Loop Exercise 8
Develop a program to display students’ average mark based on mark entered by the user.
First the program will ask user to enter number of students.
For each of the student, user need to key in their mark.
The program will repeat based on number of students entered.
System will display the average mark for all students.
21
(while and for loop)
22
Nested Loop
The cool thing about Python loops is that they can be nested i.e. we can use one or more loops
inside another loop. This enables us to solve even more complex problems.
#1) Nesting for Loops
#2) Nesting while Loops
23
Nested Loop example 1
Type the code below. Observe the output and
make sure you understand the code.
24
25
26
Nested Loop Exercise 1
Modify the previous code (nested loop example 1) to display the output as
below:
27
Nested loop answer suggestion
28
Nested loop exercise 2
Modify Loop exercise 8 so that it will Inner
consist of an inner loop that runs inside loop
the outer loop (nested loop).
In this case, you will need an inner loop outer
(number of students for each class) and loop
an outer loop (to continue or stop using
the system).
Print ‘Thank you’ when user choose to
stop using the system
Refer the output sample →
29
Nested loop exercise 2 solution
30
Nested Loop exercise 3
• Modify nested loop exercise 2 code to display the output as below:
31
Nested loop exercise 3 solution
32
Nested Loop Exercise 4
Modify the nested loop exercise
3 code to display the error
message when the user key-in
less than 0 or greater than 100
for each student’s mark.
33
Nested loop exercise 4 solution
34
Nested loop exercise 1
Write a program to calculate participants’ BMI. The program will ask the user to enter a number of
participants with their height and weight. Then the calculated BMI will be displayed on the screen.
The program repeats as long as the user enter Y to continue. Don’t forget to apply an input
validations for number of participant where the system will display an error message if user key in 0
number of participant.
35