Starter – Revisit phase
What will the following code display?
Answer:
s missing
• Error
What type of error is it?
• Syntax error
Created by Mr Suffar
Lesson 17 Saturday, May 11, 2024
Arrays & trace table
Lesson objectives…
Complete trace tables.
Solve real life problems using arrays.
Use iteration to loop through an array.
[Link]
Created by Mr Suffar
Trace table – Class demonstration:
x Output
names = ["Tom", "Mark", "Sam"]
for x in range(0,len(names)):
print(names[x])
0 Tom
1 Mark
2 Sam
Created by Mr Suffar
Trace table – Class demonstration:
x letter Output
0 x
z zy
1 y yy
2 x
z zy
Created by Mr Suffar
145. Complete the trace table
new x num
0 0 3
-3 1 4
1 2 7
8 3 1
7 4 4
11
[Link]
Created by Mr Suffar
145. Complete the trace table
new x num
0 0 3
-3 1 4
1 2 7
8 3 1
7 4 4
11
[Link]
Created by Mr Suffar
146. Complete the trace table – Assume the user enters
the following as inputs: Metallica, Idles
band name Output
N/A Metallica Metallica
Pink Metallica pink
Queen Metallica Queen
N/A Idles Idles
Beatles Idles Beatles
[Link]
Created by Mr Suffar
146. Complete the trace table – Assume the user enters
the following as inputs: Metallica, Idles
band name Output
N/A Metallica Metallica
Pink Metallica Pink
Queen Metallica Queen
N/A Idles Idles
Beatles Idles Beatles
[Link]
Created by Mr Suffar
147. Complete the trace table
highest lowest num item
0 9999 0 4
4 4 1 2
4 2 2 7
7 2 3 5
7 2 4 8
8 2 5
[Link]
Created by Mr Suffar
147. Complete the trace table
highest lowest num item
0 9999 0 4
4 4 1 2
4 2 2 7
7 2 3 5
7 2 4 8
8 2 5
[Link]
Created by Mr Suffar
148. Complete the trace table – Assume the user enters
5,2,8,1,4 as inputs total num number item
0 0 5 4
9 1 2 8
17 2 8 5
30 3 1 2
32 4 4 6
38 5
[Link]
Created by Mr Suffar
148. Complete the trace table – Assume the user enters
5,2,8,1,4 as inputs total num number item
0 0 5 4
9 1 2 8
17 2 8 5
30 3 1 2
32 4 4 6
38 5
[Link]
Created by Mr Suffar
149.
names = ["Ted","Arin","Mark","Son","Jim","Lilly"]
Create an algorithm that:
• Asks the user to pick a name
• If the name is inside the array, display, "name found" then display the index of that name.
• Use the following array: names = ["Ted","Arin","Mark","Son","Jim","Lilly"]
• Display "Name not found" if the name is not in the array.
• The process is repeated until the user enters a name that is inside the array.
Paste your code below:
Clue under here!
[Link]
Created by Mr Suffar
149.
names = ["Ted","Arin","Mark","Son","Jim","Lilly"]
Create an algorithm that:
• Asks the user to pick a name
• If the name is inside the array, display, "name found" then display the index of that name.
• Use the following array: names = ["Ted","Arin","Mark","Son","Jim","Lilly"]
• Display "Name not found" if the name is not in the array.
• The process is repeated until the user enters a name that is inside the array.
Paste your code below:
names = ["Ted","Arin","Mark","Son","Jim","Lilly"]
repeat = True
while repeat == True:
choice = input("Enter a name")
for x in range(0,len(names)):
if names[x] == choice:
print("Name found")
print(names[x] + " is on index: " + str(x))
repeat = False
if repeat == True:
print("Name not found, try again")
[Link]
Created by Mr Suffar
150. numbers = [5,2,78,8,2,5,9,4,22,66,11,88,77,38,266,12,1]
Create a program that loops through the array above and calculates and displays the
following:
• The lowest number in the array
• The highest number in the array
• The total (sum) of the numbers in the array
• The average of the numbers in the array then
• display each of the above in a full sentence.
Paste your code below:
Clue under here!
[Link]
Created by Mr Suffar
150. numbers = [5,2,78,8,2,5,9,4,22,66,11,88,77,38,266,12,1]
Create a program that loops through the array above and calculates and displays the
following:
• The lowest number in the array
• The highest number in the array
• The total (sum) of the numbers in the array
• The average of the numbers in the array then
• display each of the above in a full sentence.
Paste your code below:
numbers = [5,2,78,8,2,5,9,4,22,66,11,88,77,38,266,12,1]
highest=0
lowest = 99999
total = 0
for x in range(0,len(numbers)):
total = total+numbers[x]
if numbers[x] > highest:
highest = numbers[x]
if numbers[x] < lowest:
lowest = numbers[x]
average = total/len(numbers)
print("The total is: ", total)
[Link]
print("The average is: ", average)
print("The lowest number is: ", lowest)
print("The highest number is: ", highest)
Created by Mr Suffar
151. A survey on favourite pets has been completed. The results are stored in an array called
pets.
pets = ["Cat","Dog","Cat","Cat","Cat","Cat","Dog"]
Use the array above to create an algorithm that:
Calculates and outputs how many votes Cat received.
Calculates and outputs how many votes Dog received.
Calculates the number of votes.
Paste your code below:
Clue under here!
[Link]
Created by Mr Suffar
151. A survey on favourite pets has been completed. The results are stored in an array called
pets.
pets = ["Cat","Dog","Cat","Cat","Cat","Cat","Dog"]
Use the array above to create an algorithm that:
Calculates and outputs how many votes Cat received.
Calculates and outputs how many votes Dog received.
Calculates the number of votes.
Paste your code below:
pets = ["Cat","Dog","Cat","Cat","Cat","Cat","Dog"]
cat = 0
dog = 0
total = 0
for x in range (0,len(pets)):
if pets[x] == "Cat":
cat = cat + 1
else:
dog = dog + 1
total = total + 1
print("Total votes for Cats : ", cat) [Link]
print("Total votes for Dogs : ", dog)
print("Total votes : ", total)
Created by Mr Suffar
152. Martin is creating an algorithm to calculate how many birds he fed in the park. Add the array to your
code:
birds = ["Robin","Pigeon","Starling"]
Create an algorithm that:
• Asks the user if they fed any birds.
• If the answer is yes, ask them for the name of the bird.
• If the name of the bird is inside the array birds, then ask the user how many of that bird did they feed.
• Calculate and display the total number of birds that have been fed.
• The process repeats as long as the user enters “yes” when asked if they fed any birds.
Paste your code below:
Clue under here!
[Link]
Created by Mr Suffar
152. Martin is creating an algorithm to calculate how many birds he fed in the park. Add the array to your
code:
birds = ["Robin","Pigeon","Starling"]
Create an algorithm that:
• Asks the user if they fed any birds.
• If the answer is yes, ask them for the name of the bird.
• If the name of the bird is inside the array birds, then ask the user how many of that bird did they feed.
• Calculate and display the total number of birds that have been fed.
• The process repeats as long as the user enters “yes” when asked if they fed any birds.
Paste your code below:
birds = ["Robin","Pigeon","Starling"]
feed = input("Did you feed any birds?")
total = 0
while feed == "yes":
name = input("Enter bird name")
for x in range(0,len(birds)):
if birds[x]== name:
number = int(input("How many of that bird did you feed?"))
total = total + number
feed = input("Did you feed any other birds?")
print("Total birds fed : ",total)
[Link]
Created by Mr Suffar