Presentation 7 - Iteration
Presentation 7 - Iteration
(REPETITION)
USIN G LO O PS IN
PYTHON
1. Test # 3 Feedback
and Explanation
2. Introduction to
Iteration – For
Loops (Using all 3
Lesson parameters)
Objectives
ALL WORKSHEETS MUST BE FILED
IN YOUR FOLDERS – EACH
WORKSHEET CARRIES MARKS
B R I N G Y O U R F O L D E R S O N M O N D AY
FOR CHECKING
A L L S T U D E N T S S H O U L D H AV E
THE 4 CW WORKSHEETS FROM
THIS WEEK AND 1 HW SHEET
FROM THE TOPIC OF BINARY
NUMBERS
CLASS CR PLEASE COLLECT THESE
LESSON 1 – TEST #3
FEEDBACK
Calculating the
Lesson averages using a for
Objectives loop
How does totaling work?
total=0
for count in range(0,5):
num=int(input(“Enter a number’))
total=total + num
print(“The total is”, total)
0 0
total count
5
num
This is just an assumed
value. User can enter any
total=total + num value
5 0
total count
5
num
This is just an assumed
value. User can enter any
total=total + num value
5 1
total count
6
num
This is just an assumed
value. User can enter any
total=total + num value
11 1
total count
6
num
This is just an assumed
value. User can enter any
total=total + num value
11 2
total count
10
num
This is just an assumed
value. User can enter any
total=total + num value
21 2
total count
10
num
This is just an assumed
value. User can enter any
total=total + num value
21 3
total count
5
num
This is just an assumed
value. User can enter any
total=total + num value
26 3
total count
5
num
This is just an assumed
value. User can enter any
total=total + num value
26 4
total count
4
num
This is just an assumed
value. User can enter any
total=total + num value
30 4
total count
4
num
This is just an assumed
value. User can enter any
total=total + num value
So the final output will be 30
Averages - for loops
How does averaging work?
total=0
for count in range(0,5):
num=int(input(“Enter a number’))
total=total+num
print(“The total is”, total)
avg=total/5
print(“The average is”, avg)
Practice Q3
• There are 10 students in Year 10 at BOS. The teacher
wants to input the names of all 10 students as well
their marks in Test # 3.
• The teacher then wants to output the total of all the
marks entered and the average marks for the entire
class.
• Save this python program as FOR LOOP Q3.
Practice Q4-a
Calculating the
Lesson averages using a for
Objectives loop
Practice Q5
• In a class of 15 students, each student gives 2 tests: Math
test (out of 10 marks) and an English (out of 15 marks).
• The class teacher wishes to know the following:
1. The total of all math marks
2. The total of all English marks
3. The average marks of the Math test
4. The average marks of the English test
Output these results with suitable messages. Save your
program as FOR LOOP Q5 on Showbie
Averages - for loops
How does averaging work?
total=0
for count in range(0,5):
num=int(input(“Enter a number’))
total=total+num
print(“The total is”, total)
avg=total/5
print(“The average is”, avg)
Revision for mid-term exams
Complete Loops P6
Complete Loops P7
5
num
This is just an assumed
value. User can enter any
Is 5 > 10 ? value
0 1
count10 count
-900
num
This is just an assumed
value. User can enter any
Is -900 > 10 ? value
1 2
count10 count
50
num
This is just an assumed
value. User can enter any
Is 50 > 10 ? value
2 3
count10 count
500
num
This is just an assumed
value. User can enter any
Is 500 > 10 ? value
2 4
count10 count
3
num
This is just an assumed
value. User can enter any
Is 3 > 10 ? value
Now the loop stops
Write a program which will tell Susan whether or not classes can be
conducted for the 2 subjects.
STEPS
count_less2=0 count_less2=0
if(value<2): if(value<2):
count_less2=count_less2+1 count_less2=count_less2+value
if(value<2): if(value<2):
count_less2=count_less2+1
count_less2=count_less2+value
count_astar=0
count_a=0
count_b=0
count_c=0
count_d=0
count_e=0
count_f=0
total=total+percent
avg=total/44
print(“The total marks are”, total, “and the average marks are”, avg)
print(“The number of students who got an A* is”, count_astar)
print(“ Number of students with an A is”, count_a, “ Number of students with an B is”, count_b,“
Number of students with an C is”, count_c,“ Number of students with an D is”, count_d,“ Number of
students with an E is”, count_e, “and number of students who failed is”, count_f)
Ibrahim and Ahmad run a cafe and want to see how much money they made in a week.
The cafe sells the following:
Sandwiches $5 each
Practice Q11
Drinks $3 each
Burgers $9 each
Milkshakes $6 each
Each customer can only buy one item in a day. Write a python program which will ask the customer the item
they wish to buy (1 for sandwich, 2 for drink, 3 for burger and 4 for milkshake) and then output the following for
a week:
1. Number of customers who bought a sandwich
2. Number of customers who bought a drink
3. Number of customers who bought a burger
4. Number of customers who bought a milkshake
The total amount of money made in a week also needs to be output
STEPS TO SOLVE THIS QUESTION:
YOU CAN ALSO KEEP ON ASKING IF THERE ARE ANY MORE CUSTOMERS
INSTEAD OF TAKING THE NUMBER OF CUSTOMERS AS INPUT - FOR THIS
YOU NEED A WHILE LOOP ( WHICH WE HAVEN’T COVERED YET)
Code for one day
Answer 11
const_sandwiches= 5
const_drinks= 3
const_burgers=9
const_milkshakes=6
num_sandwiches=0
num_drinks=0
num_burgers=0
num_milkshakes=0
total_sandwiches=0
total_drinks=0
total_burgers=0
total_milkshakes=0
if(b==2):
num_drinks= num_drinks + 1
total_drinks= total_drinks + const_drinks
if(b==3):
num_burgers= num_burgers + 1
total_burgers= total_burgers + const_burgers
if(b==4):
num_milkshakes= num_milkshakes + 1
total_milkshakes= total_milkshakes + const_milkshakes
After the loop ends, output how many times did the user select each type of
operation out of 15 and then output the percentage. For example if out of 15
times, the user selected addition 5 times then the percentage of addition will be
(5/15)*100
Practice Q13
Q) Abbas and Co. company conducts a survey for 200 employees
in the Finance department.
They were emailed the following survey:
Q1) How many hours do you work overtime in a week?
a)2 b)4 c)6 d)8 e) 10 f) more than 10
Q2) Are you satisfied with the support that your manager
provides?
a) Yes b) No c) Neutral
Print this survey for all the employees and then generate a report.
In the report that you generate print the number of people
who selected each answer.
Or
51
num
This is just an assumed
value. User can enter any
Is 51 > maximum? value
51
maximum
51 1
maximum count
23
num
This is just an assumed
value. User can enter any
Is 23 > maximum? value
51
maximum
51 2
maximum count
66
num
This is just an assumed
value. User can enter any
Is 66> maximum? value
66
maximum
66 3
maximum count
11
num
This is just an assumed
value. User can enter any
Is 11>maximum ? value
66
maximum
66 4
maximum count
99
num
This is just an assumed
value. User can enter any
Is 99>maximum ? value
99
maximum
Now the loop stops
55
num
This is just an assumed
value. User can enter any
Is 55 < minimum? value
55
minimum
55 1
minimum count
79
num
This is just an assumed
value. User can enter any
Is 79 < minimum? value
55
minimum
55 2
minimum count
6
num
This is just an assumed
value. User can enter any
Is 6 < minimum? value
6
minimum
6 3
minimum count
11
num
This is just an assumed
value. User can enter any
Is 11 < minimum ? value
6
minimum
6 4
minimum count
2
num
This is just an assumed
value. User can enter any
Is 2 < minimum ? value
2
minimum
Now the loop stops
while(condition is true):
Execute commands inside the loop
count=count+1
In a while loop, you need to:
1. Specify the starting value of
count
2. Specify the increment
3. Specify the ending value of count
in the condition with while
WHILE LOOP FOR LOOP PYTHON
PYTHON CODE CODE
count=0
print(count) print(count)
count=count+1
PYTHON PYTHON
count=0 count=0
while(count<5): while(count>5):
print(count) print(count)
count=count+1 count=count+1
This code will now run 5 times but the output will be
different
count Output
1 3
2 4
3 5
4 6
5 7
while(count <=5):
How many times will the
following loops run?
1
count=1
while(count >50):
print(count)
count=count+1
0 times
count=1
while(count >=1):
print(count)
count=count+1
Infinite loop
count=19
while(count <=29):
print(count)
count=count+1
11 times
Because 19 to 29 (inclusive)
is 11
4
count=True
while(count != False):
print(count)
Infinite loop
count=1
set_x=90
set_y=84
while(count <=5 and (set_x>45 or set_y <100) ):
print(count)
count=count+1
5 times.
name='Susan'
x=1
while (name != 'Susan’):
print(x)
x=x+1
0 times
strl='bb'
count=9
while (not (strl !
= 'bb')):
print(count)
count=count+1
Infinite loop
value=False
counter=0
while(not(value) and counter <=3):
print(counter)
counter=counter+1
4 times
9
number=9.99
x=3
while (number > 4.44 or
x==4):
print("Hello")
x=x-1
Infinite times
10
count=1
while (count<=2):
print(count)
count=count+1
2 times
11
count=10
x=90
z=34
while(x>650 or count<=13):
print("Hi")
count=count+1
4 times
12
count=10
x=90
z=34
while(x>650 and count<=13):
print("Hi")
count=count+1
0 times
13
count=10
x=90
y=55
z=34
while(True and (x>650 or count<=13)):
print("Hi")
count=count+1
4 times
14
count=10
x=90
y=55
z=34
while(False and (x>650 or count<=13)):
print("Hi")
count=count+1
0 times
15
count=0
while(count<=130):
print("Hi")
count=count+1
131 times
Totaling - while loops
How does totaling work?
total=0
count=0
while(count<=4):
num=int(input(“Enter a number’))
total=total + num
count=count+1
print(“The total is”, total)
Averages - while loops
How does averaging work?
total=0
count=0
while(count<=4):
num=int(input(“Enter a number’))
total=total + num
count=count+1
print(“The total is”, total)
avg=total/5
print("The average is", avg)
Counting - while loops
How does counting work?
count=0
Grade_a=0
while(count<=4):
marks=int(input(“Enter your marks))
if( marks >=90 ):
Grade_a=Grade_a+1
count=count+1
print("The no of students who scored an A is", Grade_a)
FINDING THE
MAXIMUM/MINIMUM VALUES
USING WHILE LOOPS
Work to be submitted
Validation, Totalling,
Quiz on While loops averages, counting,
tomorrow maximum and minimum
using while loops
HACKATHON 1.0!!!
NEXT WEEK!!!
Lesson Objectives