0% found this document useful (0 votes)
32 views234 pages

Presentation 7 - Iteration

Uploaded by

rayanahmed00123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views234 pages

Presentation 7 - Iteration

Uploaded by

rayanahmed00123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 234

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

Discussion of correct answers


E&A REPORTS
1. HW 5 – Kulsum , Muhammad (handwritten answers needed)
2. HW 6 – Kulsum, Rayaan Hasan
3. HW 7 – Worksheet given last week – All students to submit today
4. CW 7 – Zohaib, Rayaan Hasan, Muhammad, Meir, Mahnoor, Kulsum (I need the worksheet also)
5. CW 8 – Submitted by everyone
6. CW 9 – Kulsum, Rayaan Hasan
7. CW 10 - Kulsum, Muhammad
8. CW 11 – Kulsum, Meir, Muhammad
9. CW 12 – Submitted by everyone
10. CW 13 + CW 12 + CW 15 – These are the 4 worksheets done last week
11. Test # 2 and Test # 3 – Given by everyone
For loop – runs a fixed number of times
Syntax

for count in range(0,5) :


print("Hello")
What is really happening?
for count in range(0,5) :​ 5
print("Hello")
count Output
0 Hello
1 Hello
2 Hello
3 Hello
4 Hello
What is really happening?
for count in range(1,6) :​ 5
print("Hello")
Count Output
1 Hello
2 Hello
3 Hello
4 Hello
5 Hello
What is really happening?
for count in range(10,15) :​ 5
print("Hello")
Count Output
10 Hello
11 Hello
12 Hello
13 Hello
14 Hello
What is really happening?
for count in range(100,105) :​ 5
print("Hello")
Count Output
100 Hello
101 Hello
102 Hello
103 Hello
104 Hello
What is really happening?
for count in range(1,5) :​ 4
print("Hello")
Count Output
1 Hello
2 Hello
3 Hello
4 Hello
What is really happening?
for count in range(1,50) :​ 49
print("Hello")
Count Output
1 Hello
2 Hello
....... Hello
49 Hello
What is really happening?
for count in range(10,20) :​ 10
print("Hello")
Count Output
10 Hello
2 Hello
....... Hello
19 Hello
What is will be the output?
for count in range(0,5) :​ 5
print(count)
Count Output
0 0
1 1
2 2
3 3
4 4
What is will be the output?
for s in range(0,5) :​ 5
print(s+1)
Count Output
0 1
1 2
2 3
3 4
4 5
What is will be the output?
for counter in range(0,5) :​ 5
print(counter-1)
Count Output
0 -1
1 0
2 1
3 2
4 3
What is will be the output?
for b in range(0,5) :​ 5
print("Number" + str(b))
Count Output
0 Number 0
1 Number 1
2 Number 2
3 Number 3
4 Number 4
What is really happening?
for count in range(0,5,1) :​ 5
print("Hello")
Count Output
0 Hello
1 Hello
2 Hello
3 Hello
4 Hello
What is really happening?
for count in range(0,5,2) :​ 3
print("Hello")
Count Output
0 Hello
1 Hello
2 Hello
3 Hello
4 Hello
What is really happening?
for count in range(0,5,-2) :​ ?
print("Hello")

Can you write code in


which a negative 3rd
parameter will give some
result?
Practice Q1
• Input the start value and the end value from the user.
• Use these values in the range function and run a for
loop.
• Inside the for loop print the value of the counter
variable.

Save this python program as FOR LOOP Q1.


Demonstrate the ability
to use for loops correctly
with all 3 parameters

Totalling values using


Lesson the for loop
Objectives
E & A R E P O R T S ( N O T U P D AT E D )
1. HW 5 – Kulsum , Muhammad (handwritten answers needed)
2. HW 6 – Kulsum, Rayaan Hasan
3. HW 7 – Worksheet given last week – All students to submit today
4. CW 7 – Zohaib, Rayaan Hasan, Muhammad, Meir, Mahnoor, Kulsum (I need the worksheet also)
5. CW 8 – Submitted by everyone
6. CW 9 – Kulsum, Rayaan Hasan
7. CW 10 - Kulsum, Muhammad
8. CW 11 – Kulsum, Meir, Muhammad
9. CW 12 – Submitted by everyone
10. CW 13 + CW 12 + CW 15 – These are the 4 worksheets done last week
11. Test # 2 and Test # 3 – Given by everyone
FOR LOOPS
You can implement a FOR loop
in python using the range
function
PYTHON The range function will
ALWAYS start from the 1st
for count in range(0,5): number but will never reach
print(count) the last number given.

It will always end at a


number 1 less than the end
limit.
THIS LOOP WILL RUN
5 TIMES
range function - for loops
How many times will the
following loops run?
1

for count in range(0,4):


print(count)
4
2

for count in range(1,14):


print(count)
13
3

for count in range(6,16):


print(count)
10
4

for count in range(2,4):


print(count)
2
5

for count in range(-1,5):


print(count)
6
6

for count in range(-10,10):


print(count)
20
7

for count in range(0,4,2):


print(count)
2
8

for count in range(1,13,3):


print(count)
4
9

for count in range(5,15,4):


print(count)
3
10

for count in range(1,10,2):


print(count)
5
11

for count in range(40,10,-2):


print(count)
15
Practice Q2
• An experiment is being conducted where a
researcher takes the midday temperature 12 times in
a span of 24 hours.
• Can you write the python code for the researcher?
• Output all the 12 temperatures taken inside the loop
• Save this python program as FOR LOOP Q2.
Totaling - for loops
Totalling values using
a for loop

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

• Input 15 integers. Output only the even integers.


• Input 10 integers. Output the integers which are odd and
positive.
• Input 25 integers. Output the integers which are even
or negative.
• Input 10 ages in a program. Output all the ages which are
between 12 and 25 inclusive.
• Input 17 integers and output the integers divisible by 9
• Save this python program as FOR LOOP Q4 a
Practice Q4-b
• John wishes to input 30 integers in a system and
wishes to know how many integers entered were
even and how many were odd.
• Write a python program for John and produce
suitable outputs.
• Save this python program as FOR LOOP Q4 b.
Totalling values using
a for loop

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

Lesson Complete Loops P1 to P5


Objectives
Introduction to counting using for
loops

Introduction to finding the


minimum/maximum using for loops

Introduction to indefinite iteration


Lesson using the while loop
Objectives
Counting - for loops
What is Counting?
Counting simply means how many times a certain data value occurs in a
process or how many times a process/action is repeated inside a loop.

Pay attention to words such as:


HOW MANY ITEMS?
COUNT HOW MANY TIMES xyz OCCURED?
STATE THE PERCENTAGE OF ……… OUT OF …….
This counting is different from the count variable which we use to
control the number of repetitions in loops
How does counting work?
count10=0
for count in range(0,5):
num=int(input(“Enter a number’))
if(num > 10):
count10=count10+1
print(“No of integers > 10 is”, count10)
0 0
count10 count

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

What will be the value of


count10 once the loop
ends?
When the loop ends

count10=2(the last value)


count=4(the last value)
num=3(the last value)
Please check this worksheet for
FOR LOOP Q6
FOR LOOP Q7

Year 10 Cover Work for Wednesday 14t


h Dec.docx
Practice Q8
John teaches Math to a class of 10th graders. There are 13 students in the class.
All students take a test out of 10 marks on Algebra
1. Output the total marks of all students
2. Output the average class marks
3. Output how many students got the following grades:
A (>=80%)
B (>=60%)
C (>=40%)
D <40%
Example
Have a look at the numbers below:
1. 100
2. 10
3. -19
4. -8
5. -44
6. -90
7. 0
8. 45
9. 46
10. 1
How many numbers are even?
How many numbers are odd?
How many numbers are positive?
How many numbers are negative?
How many numbers are multiples of 6?

HOW MANY TIMES WILL THE LOOP RUN?

What about totals?


Sample code for counting
even numbers:
count_even=0

for count in range (0,10):


number=int(input(“Enter an integer”))
if( number % 2 == 0):
count_even=count_even + 1

print(“ The number of even integers entered is”, count_even)


Sample code for counting and
totalling even numbers:
count_even=0
total_even=0

for count in range (0,10):


number=int(input(“Enter an integer”))
if( number % 2 == 0):
count_even=count_even + 1
total_even=total_even+number
print(“ The number of even integers entered is”, count_even)
print(“ The total of all the even numbers entered is”, total_even)
Practice Q9
Q) Susan wants to report how many students in her class are failing
in Math and English to the head of academics so that reinforcement
classes can be scheduled. She is told that reinforcement classes can
only be conducted if more than 15 (For testing use 5) students in each
subject require reinforcement. Passing percentage is 50.

Susan has 45 students in her class. For testing use 14

Write a program which will tell Susan whether or not classes can be
conducted for the 2 subjects.
STEPS

1. First initialize the count variables - 2


2. Start the for loop and run it 45 times
3. Inside the loop first ask for the Math percentage
4. Then ask for the English percentage
5. If the Math percentage is < 50 then increment count_math
6. Do the same for English percentage
7. Come out of the loop
8. Now start a conditional statement
9. If count_math >=15 then print math classes can be conducted
10.If count_eng >=15 then print English classes can be conducted
Answer 9
count_math=0
count_Eng=0

for count in range(0,45):


Math_percent=float(input(“Enter the math percentage: “))
Eng_percent=float(input(“Enter the English percentage: “))
if( Math_percent <50 ):
count_math=count_math+1
if( Eng_percent < 50 ):
count_Eng=count_Eng+1
if(count_math>15):
print(“Math classes can be conducted”)
else:
print(“Math classes cannot be conducted”)
if(count_Eng >15):
print(“English classes can be conducted”)
else:
print(“English classes cannot be conducted”)
What are the following codes implementing?

count_less2=0 count_less2=0

for count in range(0,99): for count in range(0,9):

value=int(input(“Enter a number”)) value=int(input(“Enter a number”))

if(value<2): if(value<2):

count_less2=count_less2+1 count_less2=count_less2+value

Do you think there is an error in any of the codes?


What are the following codes implementing? Error is in this code. It
is not counting but
instead is totaling
count_less2=0 count_less2=0 numbers less than 2

for count in range(0,99): for count in range(0,9):

value=int(input(“Enter a number”)) value=int(input(“Enter a number”))

if(value<2): if(value<2):

count_less2=count_less2+1
count_less2=count_less2+value

Do you think there is an error in any of the codes?


Practice Q10
In a history course of 44 students, each student is assigned a grade
based on his/her exam %. The grading scheme is as follows:
>=95 A*
>=90 A
>=80 B
>=70 C
>=60 D
>=50 E
<50 F
Print how many students belong to each category of grade along with
the total and average marks.
Answer 10
total=0
avg=0

count_astar=0
count_a=0
count_b=0
count_c=0
count_d=0
count_e=0
count_f=0

for count in range(0, 44):


percent=float(input(“Enter the exam percentage: “))
if(percent >=95):
count_astar=count_astar+1
elif(percent >=90):
count_a=count_a+1
elif(percent >=80):
count_b=count_b+1
elif(percent >=70):
count_c=count_c+1
elif(percent >=60):
count_d=count_d+1
elif(percent >=50):
count_e=count_e+1
else:
count_f=count_f+1

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:

1. Take the number of customers as input for one day


2. Start a loop for the number of customers first - solve the question for one day
3. For each customer ask what they want to buy (present numeric options)
4. Calculate the total for each option
5. For each option also increment the count for that option
6. In the end output the total money made on that day
7. Now put this code in another loop which will run 7 times, for each day of the week

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

a=int(input("How many customers are there :"))


for count in range(0,a):
b=int(input("What do you want to buy? 1, 2, 3 or 4?. Please note that 1= sandwiches,
2=drinks, 3 = burgers, and 4 = milkshakes : "))
if(b==1):
num_sandwiches= num_sandwiches + 1
total_sandwiches= total_sandwiches + const_sandwiches

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

total_amount= total_sandwiches + total_drinks + total_burgers + total_milkshakes


print("The total money made for the day is $",total_amount)
print("The total number of sandwiches bought is",num_sandwiches)
print("The total money made for sandwiches for one day is", total_sandwiches)
print("The total number of drinks bought is",num_drinks)
print("The total money made for drinks for one day is", total_drinks)
print("The total number of burgers bought is",num_burgers)
print("The total money made for burgers for one day is", total_burgers)
print("The total number of milkshakes bought is",num_milkshakes)
print("The total money made for milkshakes for one day is", total_milkshakes)
Code for 7 days
Practice Q12
Q) Write a python program to implement a basic calculator. Ask the user to
input 2 numbers. Then ask the user to choose -.+,/ or *. Whatever operation the
user chooses perform that operation on the 2 numbers entered.

Repeat this process 15 times.

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

In the report that you generate print the percentage of people


who selected each answer.
Answer 13
FINDING THE
MAXIMUM/MINIMUM VALUES
USING FOR LOOPS

PLEASE READ THE INSTRUCTIONS IN THE


FOLLOWING SLIDES
1)When you have not been
given a range for the values
Write down the first number in the list, and call it 'ma x' For each
number in the list if it's bigger than 'max' replace the value of 'max' by
the larger value. When we're all done, 'max' is the largest number we
found

Write down the first number in the list, and call it


'min' For each number in the list if it's smaller than
'min' replace the value of 'min’ by the smaller value.
When we're all done, 'min' is the smallest number we
found
2) When you have been given
a range for the values
From the range given to you, put the smallest value in the max variable
before starting the loop

From the range given to you, put the largest value in


the min variable before starting the loop
Maximum - for loops
Let's say you must enter
5 numbers between 0 to 100
Code for finding max when a range has
been given
maximum=0
for count in range(0,5):
num=int(input(“Enter a number’))
if(num > maximum):
maximum=num
print(“The max number is”, maximum)
0 0
maximum count

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

What will be the value of


maximum once the loop
ends?
When the loop ends

maximum=99(the last value)


count=4(the last value)
num=99(the last value)
Let's say you are asked to find
the maximum of 5 integers.
Code for finding max when no range has been
given
maximum=int(input("Enter the 1st number"))
for count in range(0,4):
number=int(input(“Enter the number))
if( number > maximum):
maximum=number

print("The maximum number entered is", maximum)


Practice Q14
• Students in a class are asked to enter numbers in a system.
The numbers should be between 5 – 500. (Validation of the
input is not required)
• There are a total of 7 students.
• Output the largest number entered by the students and
the minimum value.
Minimum - for loops
Let's say you must enter
5 numbers between 0 to 100
Code for finding min when a range has
been given
minimum=100
for count in range(0,5):
num=int(input(“Enter a number’))
if(num < minimum):
minimum=num
print(“The min number is”, minimum)
100 0
minimum count

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

What will be the value of


minimum once the loop
ends?
When the loop ends

minimum=2(the last value)


count=4(the last value)
num=2(the last value)
Let's say you are asked to find
the minimum of 5 integers.
Code for finding min when no range has
been given
minimum=int(input("Enter the 1st number"))
for count in range(0,4):
number=int(input(“Enter the number))
if( number < minimum):
minimum=number

print("The minimum number entered is", minimum)


Practice Q15
• Students in a class are asked to enter numbers in a system.
The numbers should be between 500 – 5000. (Validation of
the input is not required)
• There are a total of 7 students.
• Output the largest number entered by the students and
the minimum value.
Practice Q16
Q) Find the factors of the following numbers:
12
45
66
40
Print out the list of factors separately for each
number
Answer 16
num1=12
num2=45
num3=66
num4=40

print(“Printing factors for “, num1) print(“Printing factors for “, num3)


for count in range(1, num1+1): for count in range(1, num3+1):
if(num1%count==0): if(num3%count==0):
print(count) print(count)

print(“Printing factors for “, num2) print(“Printing factors for “, num4)


for count in range(1, num4+1):
for count in range(1, num2+1):
if(num4%count==0):
if(num2%count==0):
print(count)
print(count)
WHILE Loops
while loops
These are condition controlled loops and more
specifically pre condition loops

while(condition is true):
Execute commands inside the loop

Come out of the loop if the condition becomes false


Main difference between a for loop and while
loop in Python is that when writing a while loop
you need to increment the counter yourself -
something which happens automatically in for
loops

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

while(count<5): for count in range(0, 5):

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 LOOP WILL RUN THIS LOOP WILL RUN


5 TIMES 0 TIMES
Introduction to indefinite
iteration using the while loop

Input/Output using while


loops

Lesson Selection using while loops


Objectives
We want to run a while loop 5 times. How do
we write the code?

count=1 (you can also start from 0)


while(count <=5):
print(count)

What is the error in this code? Can you find one?


Error is that this is an infinite loop
since the count does not change in
value and will always stay 1
(meaning <=5)
We want to run a while loop 5 times. How do
we write the code?

count=1 (you can also start from 0)


while(count <=5):
print(count)
count=count+1
This code will now run 5 times
count Output
1 1
2 2
3 3
4 4
5 5
while(count <=5):
count=1 (you can also start from 0)
while(count <=5):
print(count+2)
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

Because the value of count


will never be greater than
50 for the loop to begin
2

count=1
while(count >=1):
print(count)
count=count+1
Infinite loop

Because the condition with


the while loop will always
be true
3

count=19
while(count <=29):
print(count)
count=count+1
11 times

Because 19 to 29 (inclusive)
is 11
4

Let’s try boolean values

count=True
while(count != False):
print(count)
Infinite loop

Because the value of count will


never match False and this will
make the condition with while
TRUE every time
5

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.

Because the value of set_x is going


to be > 45 every time, the value of
set_y will be < 100 every time and
the count will move from 1 to 5
6

name='Susan'
x=1
while (name != 'Susan’):
print(x)
x=x+1
0 times

Because the condition


with the while loop is
false
7

strl='bb'
count=9
while (not (strl !
= 'bb')):
print(count)
count=count+1
Infinite loop

Because the strl is


=='bb'
8

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

PLEASE READ THE INSTRUCTIONS IN THE


FOLLOWING SLIDES
1)When you have not been
given a range for the values
Write down the first number in the list, and call it 'max' For each number
in the list if it's bigger than 'max' replace the value of 'max' by the larger
value. When we're all done, 'max' is the largest number we found

Write down the first number in the list, and call it


'min' For each number in the list if it's smaller than
'min' replace the value of 'min’ by the smaller value.
When we're all done, 'min' is the smallest number we
found
Let's say you are asked to find
the maximum of 5 integers.
How does finding max work?
maximum=int(input("Enter the 1st number"))
count=0
while(count<=3):
number=int(input(“Enter the number))
if( number > maximum):
maximum=number
count=count+1
Print("The maximum number entered is", maximum)
Let's say you are asked to find
the minimum of 5 integers.
How does finding min work?
minimum=int(input("Enter the 1st number"))
count=0
while(count<=3):
number=int(input(“Enter the number))
if( number < minimum):
minimum=number
count=count+1
Print("The minimum number entered is", minimum)
2) When you have been given
a range for the values
From the range given to you, put the smallest value in the max variable
before starting the loop

From the range given to you, put the largest value in


the min variable before starting the loop
Let's say you are asked to find
the maximum of 5 ages. The
logical range of values for a
human's age will be 1-100
How does finding max work?
maximum=1
count=0
while(count<=4):
age=int(input(“Enter the age))
if( age> maximum):
maximum=age
count=count+1
Print("The maximum age entered is", maximum)
Let's say you are asked to find
the minimum of 5 ages. The
logical range of values for a
human's age will be 1-100
How does finding min work?
minimum=100
count=0
while(count<=4):
age=int(input(“Enter the age))
if( age< minimum):
minimum=age
count=count+1
Print("The minimum age entered is", minimum)
VALIDATION

While loops are also used for


validation inside other loops
Marks=float(input("Enter marks"))

while(Marks < 0 or Marks > 100):


Marks=float(input("The marks entered
were not between 0-100. Enter marks
again:"))

print(Marks, "are the marks entered")


Practice Q17
• In a program , a user is asked to input the following:
• Full name
• Age
• Use a while loop to ensure that the user enters a valid
age(The age should not be a negative number and should
be a reasonable value)
Lesson Objectives
Paper 1 corrections

Paper 2 discussion of common mistakes

QUIZ # 1 on Wednesday 25th January – While Loops

QUIZ # 2 on Monday 6th February

When should we have the hackathon?

Work to be submitted

HW # 8 has been uploaded


How to study for any assessment?
1. Go through the textbook(material + questions)
2. Go through the uploaded presentation
3. Study the topic from Teach ICT
4. BBC Bitesize GCSE Computer Science (
https://www.bbc.co.uk/bitesize/examspecs/zdqy7nb)
5. Notes taken during lessons
6. All work done on Showbie, journals, folders(tests +
quizzes etc)
Lesson Objectives

What work needs to be HW8 – While Loop – should


submitted for the next E&As? be submitted by the deadline

Validation, Totalling,
Quiz on While loops averages, counting,
tomorrow maximum and minimum
using while loops
HACKATHON 1.0!!!
NEXT WEEK!!!
Lesson Objectives

WHAT WORK NEEDS TO BE QUIZ ON WHILE LOOPS


SUBMITTED FOR THE NEXT E&AS?

You might also like