Python: Next steps DTC: Create a program that will save lists of 30 students and their results in
Loops
a test (allows user to enter the mark for each student). Calculate and output
the average mark for each student.
Do Now:
Find the WordWall activity on google classroom
or type the link below into your browser:
https://wordwall.net/play/29748/434/2753
DTC: Create a program that will save lists of 30 students and their results in
a test (allows user to enter the mark for each student). Calculate and output
the average mark for each student.
List using Loop
Python: Next steps
DTC:
DTC:Create
Createa aprogram
Pythonthat will save
program thatlists
willofsave
30 students andstudents
lists of 30 their results
andin
atheir
test results
(allows user to enter
in a test. theuser
Allow marktofor eachthe
enter student).
result Calculate and output
for each student.
the average mark for each student.
Learning objectives
• Be able to use a for() loop to step through a list
• Understand why using a list can be more efficient
than using single variables
Python: Next steps DTC: Create a program that will save lists of 30 students and their results in
Loops
a test (allows user to enter the mark for each student). Calculate and output
the average mark for each student.
while() loops
• A while() loop works like an if() statement that repeats.
And just like an if() statement, you need a rule
• Example: Get into pairs. Person A think of a number
between 1 and 10. Person B has to guess it. Keep
guessing until you get it right
• With the guessing game, did you know how many times
you would have to repeat the code until the player got it
right?
• while() loops are great when you don’t know how many
times you are going to do something
Python: Next steps DTC: Create a program that will save lists of 30 students and their results in
Loops
a test (allows user to enter the mark for each student). Calculate and output
the average mark for each student.
Task:
Reading Code, write your answer on the mini whiteboard
• What will print the first time this loop runs?
• How many times will this loop run?
Python: Next steps DTC: Create a program that will save lists of 30 students and their results in
Loops
a test (allows user to enter the mark for each student). Calculate and output
the average mark for each student.
for() loops
• for() loops are perfect when you DO know how many
times you want something to repeat
• To loop through a set of code a specified number of
times, we need to use the range() function,
• The range() function returns a sequence of numbers,
starting from 0 by default, and increments by 1 (by
default), and ends at a specified number.
for counter in range(6):
print(“The current value in variable counter: “, counter)
Python: Next steps DTC: Create a program that will save lists of 30 students and their results in
Loops
a test (allows user to enter the mark for each student). Calculate and output
the average mark for each student.
for() loops and range( ) function
• The range() function returns a sequence of numbers,
starting from 0 by default, and increments by 1 (by
default), and ends at a specified number.
for counter in range(6):
print(“The current value in variable counter: ”, counter)
Note: range(6) does
not return the
sequence of 0 to 6,
but the sequence of
0 to 5.
Python: Next steps DTC: Create a program that will save lists of 30 students and their results in
Loops
a test (allows user to enter the mark for each student). Calculate and output
the average mark for each student.
for() loops
• Suppose you want to print out all the numbers from 0 up to 10
On Replit.com, try this code:
for counter in range(10):
c
print(counter)
• Did it work?
If not, how to modify the code?
for counter in range(11):
c
print(counter)
Python: Next steps DTC: Create a program that will save lists of 30 students and their results in
Loops
a test (allows user to enter the mark for each student). Calculate and output
the average mark for each student.
for() loops
• Remember that computers count from 0
Task:
1. Try changing the program so that it prints out all the
numbers from 0 to 25
for counter in range(26):
print(counter)
2. How about printing out from 0 to 150?
for counter in range(151):
print(counter)
Python: Next steps DTC: Create a program that will save lists of 30 students and their results in
Loops
a test (allows user to enter the mark for each student). Calculate and output
the average mark for each student.
for() loops
If you run a for() loop using 2 numbers you can change
the way it works.
On Replit.com, try this code:
for counter in range(5,10):
cc
print(counter)
• What happened?
Python: Next steps DTC: Create a program that will save lists of 30 students and their results in
Loops
a test (allows user to enter the mark for each student). Calculate and output
the average mark for each student.
for() loops
for counter in range(5,10):
print(counter)
Task: Try changing the program so that it prints:
1. all the numbers from 10 to 20
for counter in range(10,21):
print(counter)
2. all the numbers from 17 to 31
for counter in range(17,32):
print(counter)
Python: Next steps DTC: Create a program that willDTC:save lists of
Create a 30 students
program and
that their
will results
save lists in
a test (allows user to enter the mark for each student). Calculate and output
Lists of 3 students and their results in a test.
the average mark for each student.
Using loops to read a List
On Replit.com, try this code:
highScore = [125,63,35,12]
for counter in range(4):
c
print("counter: ", counter)
print(highScore[counter])
Python: Next steps DTC: Create a program that willDTC:save lists of
Create a 30 students
program and
that their
will results
save lists in
a test (allows user to enter the mark for each student). Calculate and output
Lists of 3 students and their results in a test.
the average mark for each student.
Using loops to read a List
One big advantage of lists is that you can use a for
loop to step through each value
highScore[0] highScore[1] highScore[2] highScore[3]
125 63 35 12
counter = 0
c
print(highScore[counter])
Python: Next steps DTC: Create a program that willDTC:save lists of
Create a 30 students
program and
that their
will results
save lists in
a test (allows user to enter the mark for each student). Calculate and output
Lists of 3 students and their results in a test.
the average mark for each student.
Using loops to read a List
One big advantage of lists is that you can use a for
loop to step through each value
highScore[0] highScore[1] highScore[2] highScore[3]
125 63 35 12
counter = 1
c
print(highScore[counter])
Python: Next steps DTC: Create a program that willDTC:save lists of
Create a 30 students
program and
that their
will results
save lists in
a test (allows user to enter the mark for each student). Calculate and output
Lists of 3 students and their results in a test.
the average mark for each student.
Using loops
One big advantage of lists is that you can use a for
loop to step through each value
highScore[0] highScore[1] highScore[2] highScore[3]
125 63 35 12
counter = 2
c
print(highScore[counter])
Python: Next steps DTC: Create a program that willDTC:save lists of
Create a 30 students
program and
that their
will results
save lists in
a test (allows user to enter the mark for each student). Calculate and output
Lists of 3 students and their results in a test.
the average mark for each student.
Using loops
One big advantage of lists is that you can use a for
loop to step through each value
highScore[0] highScore[1] highScore[2] highScore[3]
125 63 35 12
counter = 3
c
print(highScore[counter])
Python: Next steps DTC: Create a program that will save lists of 30 students and their results in
Lists
a test (allows user to enter the mark for each student). Calculate and output
the average mark for each student.
Your turn:
• Create a list storing three subjects:
• Use a for loop to step through the list and print out each
subject at a time
subjects = ["English", "Maths", "Computer Science"]
for counter in range(3):
print(subjects[counter])
Python: Next steps
Lists
Task: Lesson 3 Workbook - SUPER VILLAINS
• Complete the google worksheet, which will give you practice
using a for loop and a list together
DTC
• Create a program that will save a list of 10 students
and their results in a test.
• Allowing the user to input each student’s result.
• Finding the mean of the test results.
Additional extras:
• Displaying the results in ascending order.
You may use the internet to research some of these if
unsure.
Python: Next steps
Lists
Help for worksheet - question 5
highScore = [125,63,35,12]
totalScore = 0
for counter in range(4):
totalScore = totalScore + highScore[counter]
print(totalScore)
Python: Next steps DTC: Create a program that willDTC:save lists of
Create a 30 students
program and
that their
will results
save lists in
a test (allows user to enter the mark for each student). Calculate and output
Lists of 3 students and their results in a test.
the average mark for each student.
Plenary
● Which loop is used to read each value from a list?
● Which number does computing start counting from?