Programming technique workbook.�Part 3
Name:
Class:
Created by Mr Suffar
Revisit phase – Starter
Log on kahoot:
https://create.kahoot.it/share/y10-python-for-loops-and-outputs/b386c8e4-ace7-434f-8837-2ef28efccebe
Created by Mr Suffar
Random numbers
Lesson 16
Monday, July 17, 2023
Lesson objectives…
Created by Mr Suffar
RANDOM NUMBERS
When working with random numbers in python, the first thing that you need to do is type the following line at the top of your program:
To display a random number between 1 and 100 INCLUSIVE, use this code :
import random
import random
num = random.randint(1,100)
print(num)
Created by Mr Suffar
RANDOM NUMBERS
The following code will store 2 random numbers then multiplies them by each other.
Created by Mr Suffar
RANDOM NUMBERS
The following code display the SAME random number 5 times. This is because the random number is generated outside the loop.
Created by Mr Suffar
RANDOM NUMBERS
The following code display the 5 DIFFERENT random numbers.
Created by Mr Suffar
Created by Mr Suffar
Tasks
Complete the tasks
Created by Mr Suffar
348. Copy the code below, run it and test it, then explain the purpose of the program/what the program does.
Explain the purpose of the code below:
import random
for x in range(1,11):
result = random.randint(0, 1)
if result == 0:
print("Coin "+str(x)+" landed on Heads")
else:
print("Coin "+str(x)+" landed on Tails")
Answer:
Created by Mr Suffar
349. The program below must: generate a random number and display it on the screen. For each random number generated, you must also ask the user to enter a separate number and display it. Multiply the two numbers together and add them to a total. Repeat the above 5 times. Display the total at the end.
Copy the code below, run it and fix the errors.
import random
total 0
for x in range(5)
num1= random.randinteger(1,100)
while True:
num2 = intinput("Enter a number between 1 and 100"))
if num2 >=1 and num2 <= 100
break
else:
print(TRY AGAIN, NOT BETWEEN 1 AND 100")
print("Num1:",num1)
print("Num2:",num)
total = total + (num1*num2
print(total)
Paste the correct version below:
Created by Mr Suffar
350. Create a program that:
Generate a random number and add it to a total that starts from 0 and display the total.
Repeat the above until the total is 1000 or above.
import random
total = ___
while _____ < 1000:
num = _____._____(1,100)
total = _____ + _____
print("Total is:",_____)
Copy the code above, fill the gaps then paste your code below:
Created by Mr Suffar
351.
Paste your code below:
Created by Mr Suffar
352. Create a program that:
Paste your code below:
Created by Mr Suffar
353.
Paste your code below:
Created by Mr Suffar
354.
Paste your code below:
Created by Mr Suffar
355. Create a program that:
Paste your code below:
Created by Mr Suffar
356. Create a program that :
Paste your code below:
Created by Mr Suffar
357. Create a program with the following criteria:
Paste your code below:
Created by Mr Suffar
358.
Paste your code below:
Created by Mr Suffar
359. Create a program that:
Paste your code below:
Created by Mr Suffar
360. Create a guessing game.
Paste your code below:
Created by Mr Suffar
361. Create a program that generates a random number between 1 and 10 inclusive.
Repeatedly ask the user to guess it until the user guesses correctly.
After each guess display whether they guessed correctly or not.
Paste your code below:
Created by Mr Suffar
362. Create a program that:
Paste your code below:
Created by Mr Suffar
363. Create a program that:
Paste your code below:
Created by Mr Suffar
Homework: Watch the video:
Created by Mr Suffar
Revisit phase – Starter
What are the 3 different programming constructs? Give an example of each.
Created by Mr Suffar
Revisit phase – Starter
Which programming construct was used in the above program?
Created by Mr Suffar
Revisit phase – Starter
Which programming construct was used in the above program?
Created by Mr Suffar
Revisit phase – Starter
Which programming construct was used in the above program?
Created by Mr Suffar
Arrays
Lesson 16
Monday, July 17, 2023
Lesson objectives…
Created by Mr Suffar
Theory:
Created by Mr Suffar
Arrays
Variable: Location in memory that stores a value which can be changed. A variable can only store 1 Value.
Array: A data structure that stores MULTIPLE data under one identifier. All of the data in the array MUST be of the same data type.
names | |||
0 | 1 | 2 | 3 |
Tom | Sam | Ron | Chi |
Array name
Index number
(Starts from 0)
Contents of the array (Data type string)
Created by Mr Suffar
Arrays
Advantages of using array?
Created by Mr Suffar
List / record
List / record: A data structure that stores multiple data. Data can be of MULTIPLE data types.
Name | Price |
Biscuit | 2.5 |
Crisps | 1.5 |
This table can be stored in a list, however it can’t be stored in an array as it contains multiple data types
Created by Mr Suffar
How to create an array
numbers = [5,4,8,3,6,1,4]
Identifier
Square brackets
Values separated by a comma
Created by Mr Suffar
Arrays & Variables
Which one is easier to read and more efficient?
Using variables:
number1 = 5
number2 = 4
number3 = 8
number4 = 3
number5 = 6
number6 = 1
number7 = 4
Using array:
numbers = [5,4,8,3,6,1,4]
Created by Mr Suffar
Arrays
Array index starts from 0.
What do you think the following code will display?
print(names[0])
print(names[2])
names = ["Rom","Ash","Sam","Ali","Mark"]
Quotation marks are needed around strings but not around integers.
Created by Mr Suffar
Arrays
Array index starts from 0.
What do you think the following code will display?
print(len(names))
print(names[len(names)])
names = ["Rom","Ash","Sam","Ali","Mark"]
Created by Mr Suffar
Arrays
Array index starts from 0.
What do you think the following code will display?
print(names[len(names)-1])
names = ["Rom","Ash","Sam","Ali","Mark"]
Created by Mr Suffar
Cold call
Created by Mr Suffar
Tasks
Complete tasks
Created by Mr Suffar
364. Copy the code below, run it and test it, then explain the purpose of the program/what the program does.
Explain the purpose of the code below:
games = ["Rainbow Siege","Crash","Rocket League"]
index = int(input("Enter index number"))
if index >=0 and index<=2:
print(games[index])
else:
print("Index out of range")
Answer:
Created by Mr Suffar
365. The program below must: Create an array with 3 games, display the first game in the array, display the number of items in the array, display the last game in the array.
Copy the code below, run it and fix the errors.
Created by Mr Suffar
366. Create a program that: Creates an array that contains 4 different colours. Ask the user if they want to see the first or last colour in the array. Display the first or last colour 10 times depending on the user’s answer.
colours = ["Red","Black","Blue","Pink"]
choice = ____("first or last colour")
if ____ == "____":
for ___ in ____(10):
____(____[0])
else:
for x in ____(10):
____(colours[3])
Copy the code above, fill the gaps then paste your code below:
Created by Mr Suffar
367)
Paste your code below:
Created by Mr Suffar
368) Create an array called numbers and store 5 numbers in the array. Display the 2nd number in the array.
Paste your code below:
Created by Mr Suffar
369)
Paste your code below:
Created by Mr Suffar
Slicing
array = [2,3,4,5]
print(array[0:2]) #displays index 0 & 1 print(array[0:3]) #prints index 0 & 1&2
print(array[1:3]) #prints index 1&2
Created by Mr Suffar
RANDOM CHOICE
random.choice can be used to select a random item from an array.
The code above will display either rock, paper or scissor.
import random
selection = random.choice ( ["Rock","Paper","Scissor"])
print(selection)
Created by Mr Suffar
Displaying items in an array
This will display all items in an array. However, this method of displaying all items in an array is not efficient especially if you have many items in an array.
films = ["Saw","Sharks","Lion King"]
King"]
print(films[0])
print(films[1])
print(films[2])
Created by Mr Suffar
Looping through an array
You can use a FOR LOOP to loop through an array.
This makes your code more efficient.
The following code will display all of the contents of the array on separate lines:
films = ["Saw","Sharks","Lion King"]
for x in range(0,3):
print(films[x])
Created by Mr Suffar
Lets see how it works in a trace table:
x | Output |
| |
| |
| |
films = ["Saw", "Sharks", "Lion king"]
for x in range(0,3):
print(films[x])
0
Saw
1
Sharks
2
Lion King
Created by Mr Suffar
Arrays
What will the following code display?
films = ["Saw","Sharks","Lion King"]
print(len(films))
Created by Mr Suffar
Arrays
You can use a FOR LOOP to loop through an array.
You can use len(films) if you are unsure how many items there are in the array.
The following code will display all of the contents in the array on separate lines:
Created by Mr Suffar
Arrays
Below is another way to display the contents of the array.
pets = ['dog', 'Cat', 'Hamster', 'Fish']
for names in pets:
print(names)
Created by Mr Suffar
Adding data to an empty array.
Adding data to an empty array.
Created by Mr Suffar
Tasks
Complete the tasks
Created by Mr Suffar
370. Create an array called singers that contains the following singers: "Rihanna","Katy","Adele","Ed"
Use 3 print commands to:
Paste your code below:
Created by Mr Suffar
371.
Paste your code below:
Created by Mr Suffar
372. Create an array called singers that contains the name of 4 teachers in your school.
Use 3 print commands to:
Paste your code below:
Created by Mr Suffar
373. The following array called games is empty: games = ["","","","",""]
Use the above array to create a program that uses a count controlled loop to ask for the name of 5 games. Add each game to the array. Display the array.
Paste your code below:
Created by Mr Suffar
374. 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:
Paste your code below:
Created by Mr Suffar
375. In python:
Paste your code below:
Created by Mr Suffar
376. In Monster Wonderland game, a player is able to move to the next by receiving an item at random. The user can either receive a “key” which will open a door. A “Sword” which help the user move through the dungeon, and a “Rope” to climb with.
Paste your code below:
Created by Mr Suffar
377. The following array called numbers is empty: numbers = ["","","","","",""]
Use the array above to create a program that uses a count controlled loop to ask for 6 numbers. Add each number to the array. Display the array.
Paste your code below:
Created by Mr Suffar
378. Create a program that counts and displays how many even numbers are found in the following array:
numbers = [5,24,62,425,46,33,42,56,2423,32]
Paste your code below:
Created by Mr Suffar
379. In python:
numbers = [5,2,5,7,4,65,7,3,3,5,6,3]
Paste your code below:
Created by Mr Suffar
380. Create a program that doubles each number in the array and stores it back in the array.
For example the following area:
Paste your code below:
Created by Mr Suffar
381. numbers = [52,42,42,543,63467,457,6545,2]
Create a program that loops through the array above and calculates and displays the following:
Paste your code below:
Created by Mr Suffar
382. numbers = [700000,500000,2222233,1145555,22222276,4277777,7000000]
The array above shows the number of people vaccinated against Covid19 from day 1 to day 7 where index 0 represents day 1.
Create an algorithm that:
Paste your code below:
Created by Mr Suffar
383. The array films contain some film names.
Paste your code below:
Created by Mr Suffar
384. The array numbers contain 10 numbers, 5 of them are odd, 5 of them are even. Using the arrays below, create a program that takes all the odd numbers from the array numbers and puts it in the odd array and takes all the even numbers from the numbers array and puts it in the even array then display the even and odd arrays.
Paste your code below:
numbers = [5,2,7,6,4,5,8,7,9,10]
odd = ["","","","",""]
even = ["","","","",""]
Created by Mr Suffar
385. goals = [0,4,2,3,1,5,2,3,6,0,3,1,0,2,3,0,3,0,1,2,3]
The above array shows the number of goals scored in each match by Aston Villa.
Create a program that loops through the array above and calculates and displays the following:
Paste your code below:
Created by Mr Suffar
Created by Mr Suffar
Starter – Revisit phase
What will the following code display?
Answer:
What type of error is it?
s missing
Created by Mr Suffar
Arrays & trace table
Lesson 17
Monday, July 17, 2023
Lesson objectives…
Created by Mr Suffar
Created by Mr Suffar
Trace table – Class demonstration:
x | Output |
| |
| |
| |
names = ["Tom", "Mark", "Sam"]
for x in range(0,len(films)):
print(names[x])
0
Tom
1
Mark
2
Sam
Created by Mr Suffar
Trace table – Class demonstration:
x | letter | Output |
| | |
| | |
| | |
| | |
| | |
0
x
z
0
zy
1
y
yy
2
x
z
2
zy
Created by Mr Suffar
Tasks
Complete tasks
Created by Mr Suffar
386. Complete the trace table
new | x | num |
| | |
| | |
| | |
| | |
| | |
| | |
Created by Mr Suffar
387. Complete the trace table – Assume the user enters the following as inputs: Metallica, Idles
band | name | Output |
| | |
| | |
| | |
| | |
| | |
Created by Mr Suffar
388. Complete the trace table
highest | lowest | num | item |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
Created by Mr Suffar
389. Complete the trace table – Assume the user enters 5,2,8,1,4 as inputs
total | num | number | item |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
Created by Mr Suffar
The code below asks the user to enter a name, if the name is inside the array, it will display Found.
Created by Mr Suffar
The code below asks the user to enter a name, if the name is inside the array, it will display Found, otherwise it will display “not found”.
Created by Mr Suffar
The code below will loop through the array. If any part of the array is empty, it will ask for a name, then adds that name to that part.
Created by Mr Suffar
390.
Paste your code below:
Created by Mr Suffar
391. A teacher wants to add his pupil names to an array. His class has 10 students. He created the array, however some names are missing.
The array has been created:
students = ["Tom","","Andy","","Lisa","","","","","Abodi"]
Create an algorithm that:
Paste your code below:
Created by Mr Suffar
392. 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:
Created by Mr Suffar
393. Mr Suffar created an array of his favourite 6 games.
The array has been created:
students = [“CSGO",“Overwatch","",“FIFA","",""]
Create an algorithm that:
Paste your code below:
Created by Mr Suffar
394. 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:
Paste your code below:
Created by Mr Suffar
395.
names = ["Ted","Arin","Mark","Son","Jim","Lilly"]
Create an algorithm that:
Paste your code below:
Created by Mr Suffar
396. 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:
Paste your code below:
Created by Mr Suffar
397. array = [66,67,68,88,77,76,89,78]
Convert each number in the above array to its ASCII letter using “chr()” then display the word on the screen in a full sentence.
Paste your code below:
Created by Mr Suffar
398. A phone number is stored in the array below.
phonenumber = [0,7,9,1,2,3,4,5,6,7,8]
Create a program that extracts each number from the phonenumber array and add it to a variable of string data type. Then display the variable in a full sentence.
Paste your code below:
Created by Mr Suffar
399. Convert the array below to a string.
Convert the following array to a single string then display it on the screen, make the code is efficient by using a count controlled loop:
array = ["H","e","l","l","o"]
Paste your code below:
Created by Mr Suffar
400. Convert (cast) all the values in the array to a string then display the new array on the screen.
For example: array = [2,5,"A","B"]
should be displayed as ["2","5","A","B"]
Paste your code below:
Created by Mr Suffar
401. Create a program that rotates the array values clockwise by 1. Stores it in an array called array2 and then display the content of array2 on the screen. For example: the array array = [5,3,7,8,2,1] can be rotated by 1 to allow array2 to be display as [1, 5, 3, 7, 8, 2].
Create an algorithm to solve the above problem using the arrays below.
array = [5,3,7,8,2,1]
array2 = ["","","","","",""]
Paste your code below:
Created by Mr Suffar
Created by Mr Suffar
Arrays & lists
Lesson 18
Monday, July 17, 2023
Lesson objectives…
Created by Mr Suffar
Created by Mr Suffar
Arrays
An arrays is a data structure which can store multiple values, however all of the data must be of the same data type.
Arrays are static data structures meaning it can not GROW or SHRINK.
Created by Mr Suffar
Arrays & Lists
However if it grows or shrinks while the program is running, it means it’s a list.
Created by Mr Suffar
Lists
#Creates an empty list/array called numbers.
numbers = [ ]
#Loops 4 times.
for x in range(4):
#Ask the user for a number
num = int(input("Enter a number"))
#Adds the number to the end of the list.
numbers.append(num)
#Displays the list
print(numbers)
Append means adding something to the end.
Created by Mr Suffar
Lists
Empty list
Adds the name to the end of the list
Created by Mr Suffar
Tasks
Complete task
Created by Mr Suffar
402. Write a program that asks the user to enter the name of 4 games and store these within an list (array). Display the list (array).
Paste your code below:
Created by Mr Suffar
403.
Paste your code below:
Created by Mr Suffar
404.
Paste your code below:
Created by Mr Suffar
405.
Write an algorithm that:
Paste your code below:
Created by Mr Suffar
406. A teacher wants to store student names in a list. He wants 2 separate lists, 1 for males and 1 for females. Create a program that:
Paste your code below:
Created by Mr Suffar
407)
Paste your code below:
Created by Mr Suffar
Sorting
array = [1,7,2,5,6]
array.sort()
print(array)
array.reverse()
print(array)
Sorts the list in number/alphabetical order.
Sorts the list in reverse order.
Created by Mr Suffar
408.
Paste your code below:
Created by Mr Suffar
409.
Paste your code below:
Created by Mr Suffar
Remove() function
names = ["Tom","Sam","Ash","Mel"]
print(names)
names.remove(names[0])
print(names)
Remove() function is used to remove specific items in the array. In this case, index 0 is removed from the array.
Created by Mr Suffar
Remove() function
names = ["Tom","Sam","Ash","Mel"]
print(names)
choice = input("enter name to remove")
names.remove(choice)
print(names)
Removes the name that the user picks, if the user types Sam, then Sam will be removed.
Created by Mr Suffar
Pop() function
names = ["Tom","Sam","Ash","Mel"]
names.pop(0)
print(names)
print(names.pop(0))
print(names)
Removes index 0 from the array
Removes index 0 from the array and also displays the value removed. “Sam”
Created by Mr Suffar
Difference between remove() and pop()
The remove() method removes the first matching element from the array.
The pop() method removes an element at a given index, and will also return the removed item.
Created by Mr Suffar
410.
Paste your code below:
Created by Mr Suffar
411. Create the following array: numbers = [5,5,2,52,6,7645,234,4,6,30]
Paste your code below:
Created by Mr Suffar
412)
Paste your code below:
Created by Mr Suffar
413.
Paste your code below:
Created by Mr Suffar
414.
Paste your code below:
Created by Mr Suffar
415.
Paste your code below:
Created by Mr Suffar
Task 416
Answer:
Created by Mr Suffar
Task 417
?
?
?
Created by Mr Suffar
Task 418
Answer:
Created by Mr Suffar
Task 419
Answer:
Created by Mr Suffar
Task 420
Answer:
Created by Mr Suffar
Task 421
Answer:
Created by Mr Suffar
Task 422
Answer:
Created by Mr Suffar
Task 423
Answer:
Created by Mr Suffar
Task 424
Answer:
Answer:
Created by Mr Suffar
Task 425
Answer:
Created by Mr Suffar
Task 426
Answer:
Answer:
Created by Mr Suffar
Task 427
Answer:
Answer:
Created by Mr Suffar
Task 428
Answer:
Created by Mr Suffar
Task 429
Created by Mr Suffar
Task 430
Answer:
Created by Mr Suffar
Task 431
Answer:
Created by Mr Suffar
Task 432
Answer:
Created by Mr Suffar
Task 433
Answer:
Created by Mr Suffar
Task 434
Answer:
Created by Mr Suffar
Task 435
?
?
Created by Mr Suffar
Task 436
Answer:
Created by Mr Suffar
Homework watch the video:
Created by Mr Suffar
Revisit phase - Starter
Define the following terms:
Array:
List / Record:
Created by Mr Suffar
2 Dimensional Array
Lesson 19
Monday, July 17, 2023
Lesson objectives…
Created by Mr Suffar
Theory:
Created by Mr Suffar
2 Dimensional Array
2D array is an array inside another array.
It can be visualised as a table of data.
| Column 1 | Column 2 | Column 3 | Column 4 |
Row 1 | Index: [0][0] | Index: [0][1] | Index: [0][2] | Index: [0][3] |
Row 2 | Index: [1][0] | Index: [1][1] | Index: [1][2] | Index: [1][3] |
Row 3 | Index: [2][0] | Index: [2][1] | Index: [2][2] | Index: [2][3] |
Created by Mr Suffar
2D Array
array = [ [3,2,1], [2,3,4], [7,5,2] ]
print(array[0])
What will this code display?
Row 0
Created by Mr Suffar
2D Array
array = [ [3,2,1], [2,3,4], [7,5,2] ]
print(array[0][1])
What will this code display?
Row 0 column 1
Created by Mr Suffar
2D Array
array = [ [3,2,1], [5,3,4], [7,8,2] ]
print(array[1][2])
What will this code display?
Created by Mr Suffar
2 Dimensional Array
letters = [ [ "A","B","C"],["G","S","T"] ]
| Column 1 | Column 2 | Column 3 |
Row 1 | A | B | C |
Row 2 | G | S | T |
What will the following code display?
print(letters[1][1])
print(letters[0][2])
Created by Mr Suffar
You can use a nested For Loop to loop through a 2d array
row | column | output |
| | |
| | |
| | |
| | |
| | |
| | |
0
0
A
0
1
B
0
2
C
1
0
G
1
1
S
1
2
T
Created by Mr Suffar
Looping through a 2d array
The above program calculates the total of all numbers in the array.
Created by Mr Suffar
Looping through a 2d array
The above program calculates the average grade of 3 students.
Note: You don’t need to use a nested for loop if you know which column you are extracting from.
Created by Mr Suffar
Tasks
Complete tasks
Created by Mr Suffar
437) Convert the table below into a 2D ARRAY then display it on the screen:
Paste your code below:
| Column 1 | Column 2 | Column 3 |
Row 1 | 2 | 5 | 4 |
Row 2 | 8 | 7 | 1 |
Row 3 | 7 | 3 | 9 |
Created by Mr Suffar
438.
Paste your code below:
Created by Mr Suffar
439) Complete the trace table.
row | grades[row][0] | grades[row][1] | Output |
| | | |
| | | |
| | | |
Created by Mr Suffar
440) Complete the trace table. Assume the user enters the following inputs: 15,40,20,7,55,21
x | y | num | numbers[x][y] |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
Created by Mr Suffar
441) Complete the trace table. Assume the user enters the following inputs: 25,90,50
row | Output | score | grades[row][1] |
| | | |
| | | |
| | | |
Created by Mr Suffar
442. The table on the right shows student’s names and grades
and is represented the following 2d array:
grades = [["Tom",""],["Mark",“B"],["Kim",""],["Sam",“C"],["Andy",""]]
Use the above array to create an algorithm that:
and ask the user to enter a grade for that student.
Paste your code below:
Tom | |
Mark | B |
Kim | |
Sam | C |
Andy | |
Created by Mr Suffar
443. An analyst wants to add the names of 3 singers and the number of votes they received in a 2d array.
votes = [["",""],["",""],["",""]]
Assume all of the data including the names and numbers are of string data type. Use the array above.
Create a program that:
Paste your code below:
Created by Mr Suffar
444. players = [["Ronaldo","9","N"],["Messi","10","N"],["Ramos","4","Y"]]
The above 2d arrays contain player names, player number and whether they are currently on a RED card or not. Y represents a red card.
Create an algorithm that
Paste your code below:
Created by Mr Suffar
445. The table on the right can be represented by the following 2d Array:
numbers = [[1,2,4,5],[3,4,6,7],[2,7,8,9],[8,7,1,2]]
Create an algorithm that:
numbers are present in the table.
Paste your code below:
| col1 | col2 | col3 | col4 |
Row1 | 1 | 2 | 4 | 5 |
Row2 | 3 | 4 | 6 | 7 |
Row3 | 2 | 7 | 8 | 9 |
row4 | 8 | 7 | 1 | 2 |
Created by Mr Suffar
446. numbers = [[5,12,45,6,32],[5,7,2,4,5]]
Create a program that loops the array above which has 2 rows and 5 columns and resets all the numbers in the 2d array to 0 then display the array on the screen.
Paste your code below:
Created by Mr Suffar
447. The table on the right can be represented by the following 2d Array:
numbers = [[1,2,4,5],[3,4,6,7],[2,7,8,9],[8,7,1,2]]
Create an python program that:
Paste your code below:
| col1 | col2 | col3 | col4 |
Row1 | 1 | 2 | 4 | 5 |
Row2 | 3 | 4 | 6 | 7 |
Row3 | 2 | 7 | 8 | 9 |
row4 | 8 | 7 | 1 | 2 |
Created by Mr Suffar
448- A company wants to identify the length of time in seconds that sensor were activated in a day. Data for 2 of the days are shown in the table & array.
Using the array below, create a program that:
Example: Sensors were activated for 15 seconds on 15/03/2023
Use the array below to test your program.
sensors = [ ["15/10/2023",6],["15/10/2023",9],["16/10/2023",7],["16/10/2023",9] ]
Paste your code below:
Date | timeinseconds |
15/10/2023 | 6 |
15/10/2023 | 9 |
16/10/2023 | 7 |
16/10/2023 | 9 |
Created by Mr Suffar
449. numbers = [[5.5,12.3,45.2,6.7,32.99],[5.33,7.22,2.76,4.77,5.66]]
Calculate the total of the array numbers which has 2 rows and in each row it has 5 numbers. Round the answer to the nearest whole number then display it on the screen.
Paste your code below:
Created by Mr Suffar
450. numbers = [[3,0,0,0,3],[0,3,3,3,0]]
The array above is storing the score of 2 teams in a 2d array. Each team played 5 matches. Number 3 means the team has “won” a match. Number 0 means a team has lost a match.
Create a program that loops through the 2d array to calculate the number of wins each team has then display it on the screen in a full sentence.
Paste your code below:
Team1 | Team2 |
3 | 0 |
0 | 3 |
0 | 3 |
0 | 3 |
3 | 0 |
Created by Mr Suffar
451. match = [[1,0,0,1,1],[0,1,1,1,1]]
The 2d array match contain 2 teams in a game. Each team has 5 players. The match will start when all players click on the “ready” button. When the “ready” button is clicked. The number for the player position turn from 0 to a 1.
The area match currently shows the how many players are ready in each team
Loop the array above and display how many players from team1 are NOT READY and how many players in team2 are NOT READY. Display the answer in a full sentence.
Paste your code below:
Player | Team1 | Team2 |
Player1 | Ready | Not ready |
Player2 | Not ready | Ready |
Player3 | Not ready | Ready |
Player4 | Ready | Ready |
Player5 | Ready | Ready |
Created by Mr Suffar
452. points = [[80,30],[74,88],[99,88]]
The array points contain the number of positive behaviour points and number of negative behaviour points of 3 students.
Create a program that loops through the string and calculates the total overall behaviour points of each student and display it in a full sentence.
If the total behaviour point of a student is negative, then display “You have received a detention”, if the total behaviour point is positive, then display “You have received a certificate”.
Paste your code below:
Behaviour | Student1 | Student2 | Student3 |
Positive points | 80 | 74 | 99 |
Negative points | 30 | 88 | 88 |
Created by Mr Suffar
453. The table below shows the number of new
Instagram followers of 3 celebrities.
Print(celeb[1][6]) will display 232 which is the
number of followers for celebrity 1 on Sunday.
11a) Create a 2d Array to represent the table.
Then display the number of followers for
Celebrity 0 on Monday (day 0).
Paste your code below:
| 0 | 1 | 2 |
0 | 20 | 31 | 5 |
1 | 32 | 22 | 6 |
2 | 11 | 34 | 8 |
3 | 23 | 56 | 7 |
4 | 42 | 534 | 8 |
5 | 22 | 43 | 6 |
6 | 11 | 232 | 7 |
Celebrities
Days of the
Week
(Mon-Sun)
Created by Mr Suffar
454. The table below shows the number of new
Instagram followers of 3 celebrities.
Print(celeb[1][6]) will display 232 which is the
number of followers for celebrity 1 on Sunday.
11b) Use your 2D array from previous question
And a loop(iteration) to count the total of
Followers celebrity 2 received in a week.
Paste your code below:
| 0 | 1 | 2 |
0 | 20 | 31 | 5 |
1 | 32 | 22 | 6 |
2 | 11 | 34 | 8 |
3 | 23 | 56 | 7 |
4 | 42 | 534 | 8 |
5 | 22 | 43 | 6 |
6 | 11 | 232 | 7 |
Celebrities
Days of the
Week
(Mon-Sun)
Created by Mr Suffar
455. The table below shows the number of new
Instagram followers of 3 celebrities.
Print(celeb[1][6]) will display 232 which is the
number of followers for celebrity 1 on Sunday.
11c) Use your 2D array from previous question
And a nested loop (2 for loops) to calculate &
Display the total followers of all 3 celebrities
In a week (7 days).
Paste your code below:
| 0 | 1 | 2 |
0 | 20 | 31 | 5 |
1 | 32 | 22 | 6 |
2 | 11 | 34 | 8 |
3 | 23 | 56 | 7 |
4 | 42 | 534 | 8 |
5 | 22 | 43 | 6 |
6 | 11 | 232 | 7 |
Celebrities
Days of the
Week
(Mon-Sun)
Created by Mr Suffar
456. player = [["A",65],["B",68],["C",72],["D",46],["E",75],["F",79],["G",90]]
(Note, for reference: player[6][1] is 90)
Write a program which:
- Finds the lowest score in the list
- Finds the highest score in the list
- Outputs the player with the lowest score and their score
- Outputs the player with the highest score and their score
Paste your code below:
Created by Mr Suffar
Revision
Lesson 20
Monday, July 17, 2023
Lesson objectives…
Created by Mr Suffar
Tasks
Complete the revision tasks
Created by Mr Suffar
457.
Paste your code below:
Created by Mr Suffar
458. Use the following array: numbers = [1,5,2,3,7,3,54,2,1,8,6,3,4]
Create a program that:
Paste your code below:
Created by Mr Suffar
459. An isosceles triangle is a triangle with (at least) two equal sides.
Paste your code below:
Created by Mr Suffar
460. Create an algorithm that:
Paste your code below:
Created by Mr Suffar
461.
goalsScored= [2,4,5,2,3,1,5,2,1,5,3,4,2,1,1,4,2,3,1,4]
goalsConceded=[3,2,3,3,0,2,1,0,4,6,2,4,2,1,3,4,2,6,1,2]
The array above shows the how many goals Aston Villa scored in each of their last 20 games and how many goals they conceded.
Create an algorithm that:
Paste your code below:
Created by Mr Suffar
462.
Ask the user for a number and store it in a variable called “num1”.
Ask the user for another number and store it in a variable called “num2”.
Swap the values of the variables so that num1 variable will contain the value of num2 and num2 contains the value of num1.
Display the final value of num1 and num2 in a full sentence.
Paste your code below:
Created by Mr Suffar
463. players = [["Sandra",7],["Bilal",4],["Hala",9]]
The above array “players” shows 3 students with their grade in a Maths competition.
Using the array above, create a program that asks the user for a name, if the name is inside the array, display the grade of the student. If the name is not inside the array, display “Not found” and repeat the question again.
Paste your code below:
Created by Mr Suffar
Paste your python code below:
image1 = [ [0,0,0],[0,1,1],[1,1,0] ]
image2 = [ [1,1,1],[1,1,0],[0,0,1] ]
inverse = True
i = 0
while i <=2:
j = 0
while j <=2:
6 Marks
Task 464
Created by Mr Suffar
Task 465
Answer:
Created by Mr Suffar
Task 466
listData = [ "","","","","","","",""]
Answer:
Created by Mr Suffar
Task 467
?
?
?
?
Created by Mr Suffar
Task 468
?
?
?
?
Created by Mr Suffar
Homework watch the video: https://youtu.be/PVLqrEVOlQM
Created by Mr Suffar
469. Starter: Revisit phase – Complete the trace table Assume the user enters the following values: 5,106,200,100
start | num | output |
True | 5 | Low |
True | 106 | High |
True | 200 | High |
True | 100 | Good |
False | | |
Created by Mr Suffar
File Handling
Lesson 21
Monday, July 17, 2023
Lesson objectives…
Created by Mr Suffar
Theory: https://youtu.be/PVLqrEVOlQM
Created by Mr Suffar
Text files
Text files can be used to save data in a text file such as score, username and passwords.
Created by Mr Suffar
Text files
Advantages of storing data on a text file:
Created by Mr Suffar
Writing to a file
file = open("test1.txt", "w")
file.write("Welcome\n")
file.write("My Friend")
file.close()
Variable /
file handler
Opens/create a file
File name
Tells the program we want to “write”
Values/text that will be inside the file.
Move to a new line.
Write inside the file.
Closes the file
Created by Mr Suffar
Example:
The code will create a file called Names.txt then it will ask the user for a name and writes that name in a text file.
Created by Mr Suffar
Example:
When writing a number to a text file. You must CAST it to a string, otherwise you will get an error.
Cast num from an integer to a string.
Created by Mr Suffar
Writing to a file
file = open("names.txt","w")
for x in range (4):
name = input("Enter name")
file.write(name+"\n")
file.close()
Variable /
file handler
Opens/create a file
File name
Repeat the process 4 times.
Asks user for a name.
Move to a new line.
Write inside the file.
Closes the file
write mode
Created by Mr Suffar
COLD CALL
Created by Mr Suffar
Tasks
Complete the tasks
Created by Mr Suffar
470. Copy the code below, run it and test it, then explain the purpose of the program/what the program does.
Explain the purpose of the code below:
file = open("dancers.txt","w")
pupil = 0
while pupil < 3:
dancer = input("Do you dance?")
if dancer=="yes":
name = input("Enter your name")
file.write(name+"\n")
pupil = pupil + 1
else:
print("Why do you not dance?")
file.close()
Answer:
Created by Mr Suffar
471. The program below must: Creates a file called “colours.txt”, ask the user for 6 colours and add them to the text file. Close the text file.
Copy the code below, run it and fix the errors.
file_name = "colours.txt
file = open(file_name, "ws")
for i in range(6)
colour = inpu("Enter colour")
file.wrie(colour + "\n"
file.closee()
print("Colours have been written to", filename)
Paste the correct version below:
Created by Mr Suffar
472. In python: Create a text file called “numbers.txt”, ask the user to enter a number and store it in a text file. Then ask them if they want to enter another number, if the answer is “no” then stop the
file = open("____.txt","__")
while True:
number= ____(____("Enter a number to store in the file?"))
file.____(str(____)+"\n")
repeat = ____("Do you want to enter another number?")
if repeat == "____":
break
file.____()
Copy the code above, fill the gaps then paste your code below:
Created by Mr Suffar
473. In python
Paste your code below:
Created by Mr Suffar
474. In python
Paste your code below:
Created by Mr Suffar
475. In python
Paste your code below:
Created by Mr Suffar
476. In python
Paste your code below:
Created by Mr Suffar
477. In python
Paste your code below:
Created by Mr Suffar
478. In python
Paste your code below:
Created by Mr Suffar
479. In python
Paste your code below:
Created by Mr Suffar
480. In python
Paste your code below:
Created by Mr Suffar
481. A teacher wants to write the names and grades of his students in a text file:
Paste your code below:
Created by Mr Suffar
482. In python:
Paste your code below:
Created by Mr Suffar
Homework watch the video:
Created by Mr Suffar
Revisit phase – Starter – Identify the 5 errors in the code.
The purpose is to ask the user for a name and write it to a text file.
Missing .txt
Should be 1 parenthesis
Should be write
Should be name without an s.
Missing ()
Created by Mr Suffar
File Handling
Lesson 22
Monday, July 17, 2023
Lesson objectives…
Created by Mr Suffar
Theory:
Created by Mr Suffar
Appending to a text file
When you open a file in write mode, it will either create a new file for you or overwrite an existing file with the same name.
If you want to open an existing file but don’t want to overwrite the contents that is already inside it, you need to open it in APPEND MODE.
This will allow you to add data to the end of the file rather than overwrite existing content.
Created by Mr Suffar
Appending to a file
file = open("test1.txt", "a")
file.write("Welcome\n")
file.write("My Friend")
file.close()
Variable /
file handler
Opens/create a file
File name
Tells the program we want to “append”
Move to a new line.
Write inside the file.
Closes the file
Created by Mr Suffar
Reading from a text file
file= open("hobby.txt", "r")
print(file.read())
file.close()
Variable/ file handler
Opens the file
File name
Read mode
Closes the file
Displays the content of readit
Created by Mr Suffar
Reading from a text file
file= open("names.txt", "r")
readit = file.read()
file.close()
print(readit)
Variable/ file handler
Opens the file
File name
Read mode
Read the content of the file and stores it in a variable called readit.
Closes the file
Displays the content of readit
Created by Mr Suffar
Reading from a text file
Before you can read from a text file, you must first check the file exists.
If the file does not exist, then an error will occur.
Created by Mr Suffar
COLD CALL
Created by Mr Suffar
Tasks
Complete tasks
Created by Mr Suffar
483.
Paste your code below:
Created by Mr Suffar
484.
Paste your code below:
Created by Mr Suffar
485.
Paste your code below:
Created by Mr Suffar
486. For this question, copy and paste your code from question 192 and put it at the top. You will be adding to it now.
Paste your code below:
Created by Mr Suffar
487.
Paste your code below:
Created by Mr Suffar
488.
Paste your code below:
Created by Mr Suffar
489.
Paste your code below:
Created by Mr Suffar
490.
Paste your code below:
Created by Mr Suffar
491. Part 1 - A company hired Jim to watch 5 films then rate each film out of 10.
Create an algorithm that:
Paste your code below:
Created by Mr Suffar
492. Part 2 – Update your previous program to allow the user to choose if they want to write the film names and rating of films that had a score of 8 or above to a file or not.
Paste your code below:
Created by Mr Suffar
Reading from a text file
file= open("names.txt", "r")
name = input("Enter name")
for line in file:
if line == name +"\n":
print("Found")
file.close()
Variable/ file handler
Opens the file
File name
Read mode
Loops through each line in the text file.
Closes the file
if the name is in the file, then it will display Found.
Created by Mr Suffar
493.
Paste your code below:
Created by Mr Suffar
Homework watch the video: https://youtu.be/ualSFRIclz8
Created by Mr Suffar
Revisit phase / Starter
What are the 3 programming constructs:
Created by Mr Suffar
Subroutine: Procedure
Lesson 23
Monday, July 17, 2023
Lesson objectives…
Created by Mr Suffar
Theory: https://youtu.be/ualSFRIclz8
Created by Mr Suffar
Procedure
def names():
choice=input("Enter a name")
print(choice)
names()
names()
def is a command which allows you to define a procedure or a function.
procedure name
Ask the user to input a name.
Displays the user’s answer.
Calls the procedure. This will run line 2 and 3.
Created by Mr Suffar
Procedure
def welcome(name):
print("Welcome to school" , name )
welcome("Alex")
welcome("Tom")
def is a command which allows you to define a procedure or a function.
Procedure name.
Displays welcome to school + the value of the argument.
Argument.
Calls the procedure. This will run line 2.
Parameter.
Created by Mr Suffar
Procedure
Local Variable
What will the program display?
def calculate(num,num2):
print(num * num2)
calculate(2,4)
calculate(5,6)
Parameters
The values 2 and 4 are passed into num and num2.
Created by Mr Suffar
Procedures
Created by Mr Suffar
Procedures
Subprogram: A self-contained piece of code that can be called from the main program. It returns control to the main program when complete and can have parameters and values sent to it.
Created by Mr Suffar
Procedures
Why we use subroutine/subprograms?
Created by Mr Suffar
Advantages of subroutine
Created by Mr Suffar
Procedure trace table - Assume the user enters the following values: 5,18,15,100,17
choice | num | output |
| | |
| | |
| | |
| | |
| | |
5
7
Bad
18
20
Good
15
17
Almost
100
102
Good
17
19
Almost
Created by Mr Suffar
Tasks
Complete tasks
Created by Mr Suffar
494. Copy the code below, run it and test it, then explain the purpose of the program/what the program does.
Explain the purpose of the code below:
def areaofrect(length,width):
area = length * width
print("Area of rectangle is",area)
areaofrect(5,7)
Answer:
Created by Mr Suffar
495.
Paste your code below:
Created by Mr Suffar
496.
Paste your code below:
Created by Mr Suffar
497. The program below must: Create a procedure that takes Celsius as parameter. It then converts Celsius to Fahrenheit and displays the answer on the screen.
In the main program, it asks the user for the temperature in Celsius and passes it as an argument to the procedure.
Copy the code below, run it and fix the errors.
define celsius_to_fahrenheit(celsius:
fahrenheit (celsius * 9/5) + 32
print("The temperature in Fahrenheit is:" fahrenheit)
temp_celsius = float(input("Enter the temperature in Celsius: ")
celsius_to_fahrenheit(tempcelsius)
Paste the correct version below:
Created by Mr Suffar
498. In python: Create a procedure called square_number() that takes a number as a parameter. In the main program, ask the user for a number and pass it as an argument to the procedure. The procedure calculates and displays the square of the number passed into the parameter.
def square_number(____):
square = ____ ** ____
print("The square of", ____, "is:", ____)
num = ____(____("Enter a number: "))
square_number(____)
Copy the code above, fill the gaps then paste your code below:
Created by Mr Suffar
499.
Paste your code below:
Created by Mr Suffar
500.
Paste your code below:
Created by Mr Suffar
501.
Paste your code below:
Created by Mr Suffar
502. Procedure trace table - Assume the user enters the following values: 88,155,15,102,100
Repeat | Repeat == True | num | output | num==100 |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
Created by Mr Suffar
503. Procedure trace table - Assume the user enters the following values: fortnite, minecraft, overwatch
x | game | age | output |
| | | |
| | | |
| | | |
Created by Mr Suffar
504.
Paste your code below:
Created by Mr Suffar
505.
Paste your code below:
Created by Mr Suffar
506.
Paste your code below:
Created by Mr Suffar
507.
Paste your code below:
Created by Mr Suffar
508.
Paste your code below:
Created by Mr Suffar
509.
Paste your code below:
Created by Mr Suffar
510- A program has a procedure, SaveLogs(), that stores the name of the user to an external text file. Create the procedure SaveLogs() that:
Takes the name as a string to be stored to the text file as a parameter
Takes the filename of the text file as a parameter
It creates a text file using the filename that was received from the parameter
It stores the name of the user in the text file
In the main program do the following:
Ask the user for their name and for a file name and pass them as arguments to the procedure SaveLogs().
Paste your code below:
Created by Mr Suffar
511- An online shop sells clothes and food. The shop provides free delivery if the total transaction of all items exceed 100$
Create a procedure called “onlineshopping” that takes a total which starts at 0 as a parameter. The procedure must:
Ask the user for number of items to be purchased. For each item, ask for the name of item and the price of the item. Calculate the total price of all items then display whether the delivery will be free or not.
Test your program by calling the procedure “onlineshopping” and using the argument 0.
Paste your code below:
Created by Mr Suffar
Task 512
Answer:
Created by Mr Suffar
Homework
Watch the video: https://youtu.be/kZA_2c7txl0
Created by Mr Suffar
Revisit – Task 513
Answer:
Created by Mr Suffar
Subroutine: Function
Lesson 24
Monday, July 17, 2023
Lesson objectives…
Created by Mr Suffar
Theory
Watch the video: https://youtu.be/kZA_2c7txl0
Created by Mr Suffar
Functions
A function is a subroutine that returns a value.
Created by Mr Suffar
Function
Local Variable
def name():
choice = input("Enter name")
return choice
print(name() )
Calls and displays the returned value of the function
Causes the function to exit and hand back the value of choice to its caller
Function name.
Created by Mr Suffar
Function
Local Variable
def multiply(num1,num2):
answer = num1 * num2
return answer
print(multiply ( 2 , 8 ) )
Parameters
Arguments, the values 2 and 8 are passed into num1 and num2.
causes the function to exit and hand back the value of answer to its caller
Created by Mr Suffar
PROCEDURE
def numbers():
for x in range(1,101):
print(x)
numbers()
What will this procedure display?
Created by Mr Suffar
FUNCTION
def numbers():
for x in range(1,101):
return x
print(numbers())
What will this program display?
It will only display 1 and not 1 to 100 as return causes the function to break.
Created by Mr Suffar
Functions & Procedures
Similarities and differences between function and a procedure
Created by Mr Suffar
Variable Scope
Global variable: Declared outside any subroutines and accessible throughout the program.
Local variable: Declared within a subroutine. Can only be accessed in that subroutine.
Created by Mr Suffar
Variable Scope
Local
Global
Created by Mr Suffar
Variable Scope
Local
Global
Identify the local and global variables in the example below:
Created by Mr Suffar
Variable Scope
Parameters have a local scope to the sub program they’re defined in.
Local
Local
Created by Mr Suffar
Variable scopes
The differences between local and global variables:
Advantages of using local variables:
Created by Mr Suffar
Tasks
Complete tasks
Created by Mr Suffar
514. Copy the code below, run it and test it, then explain the purpose of the program/what the program does.
Explain the purpose of the code below:
def check_number(number):
if number > 0:
return "Positive"
elif number < 0:
return "Negative"
else:
return "Zero"
num = float(input("Enter a number: "))
result = check_number(num)
print("The number is:", result)
Answer:
Created by Mr Suffar
515. The program below must: a
Copy the code below, run it and fix the errors.
def convert_to_miles(kilometers):
miles = kilometer * 0.621371
returns miles
km = real(input("Enter the distance in kilometers: "))
result = convert_to_miles(kmm)
print("The distance in miles is:", result
Paste the correct version below:
Created by Mr Suffar
516. In python: a
def calculate_total(____):
total = ____
for x in range(____):
num = ____(____("Enter a number"))
total = ____ + ____
return ____
num_list =____(____("How many numbers do you want to enter"))
result = calculate_total(____)
print("The total sum is:", ____)
Copy the code above, fill the gaps then paste your code below:
Created by Mr Suffar
517.
Paste your code below:
Created by Mr Suffar
518.
Paste your code below:
Created by Mr Suffar
519.
Paste your code below:
Created by Mr Suffar
520.
Paste your code below:
Created by Mr Suffar
521.
Paste your code below:
Created by Mr Suffar
522.
Paste your code below:
Created by Mr Suffar
523. Create a function called “game” that takes “playerlevel “ as a parameter. The game adjusts player level to make it fair for all players in a player vs player match. The maximum level a player can be in this match is 60.
Your function must:
Paste your code below:
Created by Mr Suffar
524.
Create a function called “game” that takes a number as a parameter.
The function must:
Paste your code on the below:
Created by Mr Suffar
525. A buffet restaurant wants to create an algorithm to calculate the price of group ticket. The restaurant charges £20 per adult and £12 per child between 8 (8 am) and 15 (2.59 pm). The price increases by 50% between 15 (3pm) and 20 (8pm). The restaurant is closed between 8pm to 8am.
Create a function called “price” that takes number of adults and number of children as parameters.
Your function must:
Paste your code on the below:
Created by Mr Suffar
526. Copy the code below, run it and test it, then explain the purpose of the program/what the program does.
Explain the purpose of the code below:
length = 200
width = 100
def area():
shapeArea = length * width
print("Area = ",shapeArea)
def perimeter():
shapePerimeter = length*2 + width*2
print ("Perimeter = ", shapePerimeter)
while response not in ("a","p"):
response = input("Do you want to calculate area or perimeter? Enter a or p" )
if response == "a" or "A":
area()
elif response == "p" or "P":
perimeter()
Answer:
Created by Mr Suffar
527. Create a function called regularcuboid() that calculates the volume of a regular cuboid.
In the main program you must ask the user for length, width and height. Pass these as argument to the function.
Inside the function calculate and return the volume of the regular cuboid.
Volume = length * width * height
Paste your code on the below:
Created by Mr Suffar
528. Create a function called “over100” that takes in the total as a parameter. The first value of the total is 0.
The function will repeatedly ask the user for a number, and adds it to the total until the total is above 100.
The function returns the total once the total is above 100.
Paste your code on the below:
Created by Mr Suffar
529. Create a function called “total” that takes in 4 numbers as parameter. The function calculates and returns the total of the 4 numbers.
In the main program, create a variable called answer and store the total that is being returned by the function, use 3 , 2, 6, 8 as arguments. Calculate the average of the 4 numbers in the main program and display the answer.
Paste your code on the below:
Created by Mr Suffar
530. Eatallyouwant restaurant charges an entry fee of £10 for children and £20 for adults.
Create a function called “buffet” that takes in the number of adult and number of children as parameters.
The function will then return the total price that needs to be paid.
Paste your code on the below:
Created by Mr Suffar
531. Create a function called totalling that takes a total which starts at 0 as parameter. The function asks for 10 numbers, add it to the total parameter then once all 10 numbers have been entered, it then return the total.
Test your program by outputting the totalling(0) function.
Paste your code on the below:
Created by Mr Suffar
Task 532
?
?
?
?
?
Created by Mr Suffar
Task 533
Answer:
?
?
?
Created by Mr Suffar
Task 534
Answer:
Answer:
Created by Mr Suffar
Task 535
Answer:
Created by Mr Suffar
Task 536
Answer:
Created by Mr Suffar
537. Create a function called “encryptstring” that takes a sentence as a parameter.
The function will replace the letters a,b and c with “#” and store it in a variable called newsentence and then return newsentence once all appropriate letters have been replaced.
In the main program, ask the user for a sentence and pass it as an argument. Print your function call to test your program.
Paste your code on the below:
Created by Mr Suffar
538. Create a function called ATM that takes a pincode which contains 4 characters as parameter.
Paste your code on the below:
Created by Mr Suffar
539. Circuit power can be calculated using the following formula: Power= Voltage * Current.
Create a function that takes voltage and current as parameter, calculates and returns the circuit power.
Test your program by outputting the function with voltage as 1.5 and current as 600.
Paste your code on the below:
Created by Mr Suffar
540. Create a function called repeatstring that takes a string and a number n as parameters and returns the repeated string n number of times.
For example, if we call the function with print(repeatstring("Tom",3)) it should display: “TomTomTom”
Test the program by printing the function call using the following arguments: “Suffar”,3
Paste your code on the below:
Created by Mr Suffar
541. Create a function called makeTen that takes two numbers as parameters. Return true if one of them is 10 or if their sum is 10. Otherwise return false.
Example: makeTen(9,10) returns True
makeTen(1,9) returns True
Paste your code on the below:
Created by Mr Suffar
542. Create a function called “shortest” that takes 2 names as parameters and return the shortest name, if both names are equal to each other in length then join the 2 names together using concatenation and return the new name which is both named joined together.
Paste your code on the below:
Created by Mr Suffar
Task 543
Paste your code below:
Created by Mr Suffar
Task 544
?
?
?
?
Created by Mr Suffar
Task 545
?
?
Created by Mr Suffar
Task 546
✓
✓
✓
✓
Created by Mr Suffar
Task 547
✓
Created by Mr Suffar
Task 548
✓
Created by Mr Suffar
Task 549
Answer:
Answer:
Created by Mr Suffar
Task 550
✓
Created by Mr Suffar
Task 551
Answer:
Created by Mr Suffar
Task 552
?
?
?
Created by Mr Suffar
year
3
return
Task 553
Answer:
Created by Mr Suffar
Task 554
Answer:
Created by Mr Suffar
Task 555
Answer:
Answer:
Answer:
Created by Mr Suffar
Task 556
Answer:
Created by Mr Suffar
Task 557
Answer:
Created by Mr Suffar
Task 558
Answer:
Created by Mr Suffar
Task 559
Answer:
Created by Mr Suffar
Task 560
Answer:
Created by Mr Suffar
Task 561
Answer:
Created by Mr Suffar
Task 562
Answer:
Created by Mr Suffar
Homework
Watch the video: https://youtu.be/GI-aBknEQ-8
Kahoot:
https://create.kahoot.it/share/programming-definitions/d4861a25-da4a-44c5-9b0e-51ded637d93b
Created by Mr Suffar
Revisit phase
1- Describe the differences between local and global variables
2- Explain why it is better to use local variables?
Created by Mr Suffar
Validation, authentication & verification
Lesson 25
Monday, July 17, 2023
Lesson objectives…
Created by Mr Suffar
Theory
Watch the video: https://youtu.be/GI-aBknEQ-8
Created by Mr Suffar
Authentication
Authentication: Used to confirm the identity of a user before they can access a program.
Example: Checking both the username and password are correct.
Created by Mr Suffar
Verification
Verification: Used to double-check that the data has been typed in correctly. This is done by checking the input matches an input that has already been supplied.
Example: Double entry of password.
Created by Mr Suffar
Validation
Validation: Checking data entered is reasonable and meets a certain criteria.
Reasons:
Types of validation:
Created by Mr Suffar
Validation – Range check
Range check: Checks if the data is within certain range.
Example: Number between 1 and 100
Created by Mr Suffar
Validation – Length check
Length check: Checks the length of the string is valid (acceptable).
Example: Password with at least 8 characters.
Created by Mr Suffar
Validation – Presence check
Presence check: Checks that data has actually been entered.
Example: Checks if the username has been entered.
Created by Mr Suffar
Validation – Type check
Type check: Checks that the data entered is of valid (acceptable) data type.
Example: We can use “DIV 1” to find out if a number is a whole number (Integer)
Created by Mr Suffar
Validation – format check
Format check: Checks that the data entered is of the correct format.
Example: Email must have an @ symbol. DOB should be xx/xx/xxxx.
Created by Mr Suffar
Tasks
Complete tasks
Created by Mr Suffar
563. Range check validation program:
A football stadium makes discounts for group tickets. Carmen wants to purchase a group ticket. Minimum number of tickets must be 5 and a maximum of 20 for a group ticket.
Create a program that:
Paste your code below:
Created by Mr Suffar
564. Length check validation program:
A website require users to have usernames between 8 and 15 characters.
Create a program that:
Paste your code below:
Created by Mr Suffar
565. Presence check validation program:
A website require users to enter a name when making an order. The website owner wants an algorithm that checks if the user has entered something when asked for a name.
Create a program that:
Paste your code below:
Created by Mr Suffar
566. Type check: Ask the user for age and set the data type to float. If the user enters a decimal number, display “Age can not contain a decimal number”. If the user enters a whole number, display “Valid data type entered”.
Paste your code on the below:
Created by Mr Suffar
567. Format check: Ask the user for date of birth. Then ensure the DOB entered is of the correct format. Ensure that it contains 2 numbers followed by / followed by 2 numbers followed by / followed by 4 numbers.
Example: 04/05/1993
Paste your code on the below:
Created by Mr Suffar
568. Verification program:
Create a program that:
Paste your code below:
Created by Mr Suffar
569. Authentication program:
Create a program that:
Paste your code below:
Created by Mr Suffar
Task 570
Answer:
Created by Mr Suffar
Extension
Complete tasks
Created by Mr Suffar
try / except
This is a very powerful piece of Python code – it basically tells Python not to crash, but to run a different block of code if it comes across an error.
This is how it works :
try:
code goes here
more code here
except :
if the above code crashes then
this code will be run instead
Created by Mr Suffar
571. Type check validation program:
Create a program that:
Created by Mr Suffar
Homework
Created by Mr Suffar
End of unit test
NOW:
Complete end of unit assessment.
MS Form assessment - test link: https://forms.office.com/Pages/ResponsePage.aspx?id=NRrmRxWbmk-P3e1XFrwgvUKZUnHi_FpMtvQIsO11-sxUNFk4NVE1TkhDQVU4RkU4MUIzUThWRDY1Ti4u
THEN:
Complete any unfinished tasks from previous lessons.
Lesson 27
Created by Mr Suffar
Grade 9 level questions
Lesson 28
Monday, July 17, 2023
Lesson objectives…
Created by Mr Suffar
572. Ask user for 10 numbers between 1 and 100 and store them in the array called numbers. Validate that only numbers between 1 and 100 are added to the array. Once all 10 numbers are in the array, display the highest and lowest number in the array and the total of the numbers in the array. Write the total, lowest and highest to a text file called "numbers.txt“
numbers = ["","","","","","","","","",""]
Paste your code below:
Created by Mr Suffar
Task 573
Paste your code below:
Created by Mr Suffar
Task 574
Paste your code below:
Created by Mr Suffar
Task 575
Paste your code below:
Created by Mr Suffar
Task 576
Paste your code below:
Created by Mr Suffar
577.
Mr Suffar is developing an adding game. Write an algorithm to play this game. The rules for the game:
players= [ ["",""],["",""],["",""],["",""],["",""]]
Paste your code below:
Example of the array at the end of the program
Created by Mr Suffar
578- A company wants to identify the length of time in seconds that sensor were activated in a day. Data for 2 of the days are shown in the table & array.
Using the array below, create a program that:
Example: Sensors were activated for 15 seconds on 15/03/2023
Use the array below to test your program.
sensors = [ ["15/10/2023",6],["15/10/2023",9],["16/10/2023",7],["16/10/2023",9] ]
Paste your code below:
Date | timeinseconds |
15/10/2023 | 6 |
15/10/2023 | 9 |
16/10/2023 | 7 |
16/10/2023 | 9 |
Created by Mr Suffar
579.
Paste your code below:
Created by Mr Suffar
Task 580
Paste your code below:
Created by Mr Suffar
Task 581
Paste your code below:
Created by Mr Suffar
Task 582
Paste your code below:
Created by Mr Suffar
Task 583
Paste your code below:
Task 584
Paste your code below:
Created by Mr Suffar
585. Coin flip game: In this program, you will create a flip the coin game where the user has to bet a specific amount, flip the coin and then win/lose the amount they bet depending if they get it right/wrong.
Paste your code below:
Created by Mr Suffar
Task 586
Paste your code below:
Created by Mr Suffar
Task 587
Paste your code below:
Created by Mr Suffar
Task 588
Paste your code below:
Created by Mr Suffar
Task 589
Paste your code below:
Created by Mr Suffar
590. Create a program that loops through the 2d array/list called marks and displays the name of the students in alphabetical order and then calculates the total marks, highest mark, lowest mark and average mark of all students in the array/list.
marks = [ ["Max",70],["Bilal",87],["Ruth",67],["Shea",57],["Ali",88]]
Paste your code on the below:
Max | Bilal | Ruth | Shea | Ali |
70 | 87 | 67 | 57 | 88 |
Created by Mr Suffar