0% found this document useful (0 votes)
71 views

Programming Workbook FSH

Uploaded by

nhan079512
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views

Programming Workbook FSH

Uploaded by

nhan079512
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 515

Programming technique workbook.

Python 1 section

Name:

Class:
Lesson 1 Sunday, June 30, 2024

Python Introduction
Lesson objectives…
 Understand how to setup Python IDLE.
 Use the print and input functions successfully.
 Explain the purpose of variables & constants.
Python Introduction
Starter activity
Python Shell
• Create a folder in your area
named: Python.

• Create another folder inside the


Python folder and name it:
Lesson 1.
Editor
Window
• Open IDLE Python. Click on FILE
 New FILE.
Video
Setting up your IDE
https://youtu.be/KLcPJQPwTjU
Python Introduction
• Click on options > Configure
IDLE

• Change the font size to 25 and


tick BOLD then click APPLY
then click on OK
Python Introduction
• Click on options > Configure
IDLE

• Click on GENERAL then click


on Open Edit Window then
click on APPLY and press OK.
Video
Lesson 1 - Theory
https://www.youtube.com/watch?v=HY7roHmN8vo
Print function
Whenever you want to display something on the screen,
you must use the print command. You must ALWAYS
open and close the PARANTHESIS.
Syntax error
Syntax: The order of instructions or commands.
Syntax Error: When you break the rules of the
programming language.
Examples:

• Print(“Welcome Tom”)

• print(“Football”

• print(Dancing)
Print function
1- Type the following in python:

2- Press F5 to run the program.


3- Create a program that displays your name, your
country and date of birth on the screen on separate
lines. Hint: Use 3 print commands.
Variables & Constants
Variable: A location in a Constant: A location in a
memory where data is memory where data is
stored. It can be changed stored. It cannot be
while the program is changed while the program
running. is running.
Variables & Constants

Value
film = “Frozen”
print (film)
Displays the
Assign film as value of the
a variable variable.
which stores a
value.
Variables & Constants

Value

Displays the
Assign film as value of the
a variable variable.
which stores
the value.

Hint: You DO NOT need Quotation mark


when storing numbers in a variable.
Variables & Constants
Concatenation: Joining things together.
In python, we can use the comma “,” to concatenate.

film = “Lion King”


print (“My favourite film is” , film)

What will the above program display?

My Favourite film is Lion King.


Variables & Constants

What will this program display?

film = “Lion King”


print (“My favourite film is” , “film”)
My favourite film is film

You must NOT put quotation marks around


variables.
Input function
Displays the
user’s answer The
question

Displays the
Variable called user’s answer
“name”

The above code will ask the user to enter their


name. Then it will wait until the user enters their
name.
Input function
Displays the
user’s answer The
question

Displays the
Variable called user’s answer
“name”

This is what the program will display if you


enter Martin.
Input function
Displays the
user’s answer The
question

Variable called
“name”

What will the program display if you enter Martin.


Your name is Martin
Python Introduction

hobby = imput(“Enter a hobby”)


print (hobby)

Syntax errors occurs when you break the


rules of the programming language.

Identify the syntax error in the code above:


• imput should be changed to input.
Tasks
Complete task 1-7. Use the hint to help
you. If you are still stuck, use the videos
in the comment section.

Then complete the extension task.


Hint:
1. Display the following on 4 separate lines:
• I like Snapchat
• I like Instagram
https://youtu.be/3G9LW5CGa7Y
• I like Twitter
• I love Facebook
Paste your code below:

Extension:
Display your name, your age and your gender on 3 separate lines.
Paste your code below:
2. Create a variable called food and store your favourite food inside
the variable. Print out the value of the variable onto the screen.
YOU DO NOT NEED AN INPUT COMMAND.
Paste your code below:
Hint:

https://youtu.be/Kf5w5MhO_t8
3. Create a program that ask the user for their hobby, surname, favourite colour
and favourite pet. Display the above on 4 separate lines. “Remember you can’t
have space in a variable name.
Hint:
Paste your code below:
https://youtu.be/V3GIhjCrnm0
4) Ask the user for their favourite film. Display “I also like
watching”, film. Hint
Paste your code below:

https://youtu.be/Ru34WIgnZ08
5. Create a program that ask the user for their name. Display the
name 1000 times. Use * to multiply. Hint:
Example:
https://youtu.be/fZEUPpQ6gNQ

Paste your code below:


6. Create a program that ask the user for their favourite food.
Display “I also like”, food. Food should be replaced with the user’s
answer. hint:
Paste your code below:

https://youtu.be/hpCJTgMdTb0
7. Ask the user for their name and favourite hobby. Display “your
name is”, name, “and your favourite hobby is”, hobby. Example:

https://youtu.be/fr7nBwIEsOs
Paste your code below:
8. Ask the user for a username. Then display “Welcome”, username.

https://youtu.be/PIdZZSV9jVU
Paste your code below:
9. Ask the user to enter a random symbol such as #. Then display
that symbol 100 times.

Paste your code below:


https://youtu.be/UvsF0Nx5rYs
Homework
Video of data types:
Watch the video: https://youtu.be/ka3nBNi-j9M
Revisit phase/ starter
Create a program that asks the user if they like
school or not, then display the answer.
Lesson 2 Sunday, June 30, 2024

Data types
Lesson objectives…
 Compare different data types.
 Create a program using variables and input command.
 Consolidate understanding of inputs, data types and
variables.
Data types
Lesson theory video:

https://youtu.be/ka3nBNi-j9M
Knowledge phase

Data type: A description about the type of data a variable


holds.
Why do we use data types?
The system will need to know the data type of the
variable so that it can allocate the correct size of memory
for the variable's data.

The computer will be unable to carry out the correct


calculations on the variables unless it knows what type of
data they contain.
Integer
An integer is a whole number.

Examples:

age = 14 number = -4

score = 75 applenumber = 3

siblings = 5
Real/Float
Real numbers can have a decimal place.

Examples:

Temperature = 18.5 Price = 1.84

weight = 7.5 Number= -4.65


Character
Holds a single alphanumeric character.

Examples:

Symbol = % Grade = C

Gender = M Initial = B
Boolean
Holds either TRUE or FALSE

Examples:

Denied = True Access= False

Repeat = True Underweight = False


String
Holds alphanumeric characters.

Any number that starts/can start with a 0 is a


STRING. Not an INTEGER.
Examples:

Name = Brad Team = Aston Villa

Phone = 07499999222 Pin = 0415


Consolidate understanding of data type

Go on
www.kahoot.it

https://play.kahoot.it/#/k/1e607505-b2ee-4749-9dc4-5f2
03c50436d
Copy the following codetask
Python in python then click
run and put any number in.

You can see that the answer is incorrect.


This is because python assumes any input is a
string.
Python task
Now copy this:

Now you will get the correct answer.


This is because we set the data type of
number to an INTEGER.
Casting
Casting: Changing a variable from one data
type to another.

Example:

age=int(input(“How old are you?”))

age = str(age)

Above line will CAST age from integer to string.


Tasks

Complete task 10-15


10. Complete the following table: https://youtu.be/emNlYFVdFvw

Data Data Type


Age = 15

House address = 25 James road

Name = “Martha”

Phone number = 07777797007

Temperature = 15.5

Price = 10.0
Adult = True / False
Gender = M
11. Ask the user for 2 numbers then divide the first number by the
second number. Display the answer. To divide numbers use /
Paste your code below:

https://youtu.be/PsQkdSxzuNA
12. Create an algorithm to calculate the area of a rectangle. Your
algorithm must:
• Asks for the user for the width. https://youtu.be/_XLP3CO0h0Q

• Asks for the length of a rectangle.


• Calculates and displays the area of a rectangle in a FULL sentence.
Paste your code below:
13. Asks 2 users for their weight, calculate the average weight of the
2 users. Display the average weight in a full sentence.
Paste your code below:

https://youtu.be/q3MbsyeCA2U
14. Asks the user to enter 3 temperatures, calculate and display the
average and total of the 3 temperatures.

Paste your code below:


https://youtu.be/-49J5K8g1lo
15. Create an algorithm that:
• Asks the user how many meals they want to buy.
• Asks the user how much each meal cost (assume all of the drinks are the same price).
• Ask the user how many drinks they want to buy.
• Asks the user how much a drink cost (assume all of the drinks are the same price).
• Calculate and display the total cost.
Paste your code below:

https://youtu.be/xyzdYN-o9ZM
Homework: Watch the following video:
https://youtu.be/hJ0FtnWFdOE
Revisit phase / starter:
Log on kahoot
Link:
https://play.kahoot.it/#/k/1e607505-b2ee-4749-9
dc4-5f203c50436d
Lesson 3 Sunday, June 30, 2024

Arithmetic Operators
Lesson objectives…
 Understand arithmetic operators.
 Demonstrate using operators to solve problems.​
 Use operators to solve a real-world situation.
Arithmetic operators:
https://youtu.be/hJ0FtnWFdOE
Arithmetic Operators
Command Name Example Output
* Multiplication 6*5 30

/ Division 6/3 2
MOD (% in Python) Modulus / 10 MOD 3 1
modulo

^ (** in Python) Exponent 3^2 9

DIV ( // in Python) Integer 10 DIV 3 3


Division
Consolidation

Go on
www.kahoot.it
https://play.kahoot.it/#/k/c5703103-9a6e-4b44-8d72-5f7
35c75fe45

Game pin will be on the board.


Commenting
• Commenting is used to allow programmers to understand the purpose of the
code.
• In python, we use # to comment on code. Examples below:
Application phase

Complete task 16-30


16. Complete the following table:
Arithmetic operator symbol
Arithmetic operator name
IN PYTHON
Addition +

Subtraction -

Multiplication

Division

Exponent “^”

Modulo “MOD”

Integer Division “DIV”


17.
• Create a variable x with a value of 5.
• Create a variable y with a value of 3.
• Create a variable z with a value 10.
• Multiply three numbers together and store them in a variable called answer.
• Display the value of answer
• Comment on the code using #.
Paste your code below:

https://youtu.be/rj-gf-m6EaY
18. Create a program that:
https://youtu.be/_z0H2aaxVJU
• Ask the user for 3 numbers.
• Create a variable called answer and store the value of the first number multiplied by
the second number.
• Create another variable called “finalanswer” and store the value of the “answer”
variable divided by the 3rd number.
• Display the value of finalanswer.
Paste your code below:
19. Create a program that:
• Ask how many apples the user wants.
• Ask how many people the user will share the apples with.
• Finds out how many apples will remain if you share the apples equally.
• Hint: use modulo % (finding the remainder).
• Display the how many apples remain in a full sentence.
Paste your code below:

https://youtu.be/VpS2tuWOfMo
20. Create a program that:
https://youtu.be/2WJEM85xxL4
• Asks for the home team name.
• Asks for the opponent team name.
• Asks for the number of goals scored by the home team.
• Asks for the number of goals scored by the opposition team.
• Calculates the goal difference for the home team. (Could be a negative difference)
• Display the goal difference in a full sentence.
Paste your code below:
21. A group of students are going to the shop to buy sweets. The students want to share
the sweets and payment evenly. The students want to donate any remaining sweets.
Create a program that:
• Asks how many sweets needed.
https://youtu.be/fK5vO5HaSEc
• Asks for the number of students.
• Asks how much the total sweets cost.
• Calculates and displays how much each student need to pay.
• Calculates and displays how many sweets each student receives.
• Calculates and displays how many sweets remain (use Modulus “%” for this)
Paste your code below:
22. Create a program that calculates how many hours, minutes and seconds are in a week.
Display each answer in a full sentence.
Paste your code below:

https://youtu.be/gXVXvD6VKFk
23. A shop owner pays 20% tax on each item they sell. Create an algorithm that:
• Asks the customer how many sweets they want to buy.
• Asks the shop owner how much each sweet costs (assume they all cost the same price).
• Calculates the total amount and adds 20% on top and stores it in a variable called after
“finaltotal”.
• Display the total amount after tax in a full sentence. https://youtu.be/XtpYL3GKmq4
Paste your code below:
24. A user wants to measure the length of his car. Create an algorithm that:
• Asks the user to enter the length in CM. https://youtu.be/Ohrs6j5Qk_M
• Convert the length to inches. (0.393701 INCH in 1 CM)
• Display the length in inches.
Paste your code below:
25. The following formula is used to convert Celsius to Fahrenheit: Y°F =(X°C × 9/5) + 32
• X represents the value in Celsius and Y represent the value in Fahrenheit.
• Use the formula above to create an algorithm that converts Celsius to Fahrenheit.
Your algorithm must:
• Ask the user for the temperature in Celsius.
• Converts the temperature into Fahrenheit.
• Displays the answer in a full sentence.
Paste your code below:
https://youtu.be/bVE-HmXHPZc
26. A shop wants to create an online payment system using gift cards only.
Create an algorithm that: https://youtu.be/4F3Kqzqf0QU
• Asks how much the customer’s items cost in total.
• Asks the customer if they are paying using £20 gift card, £40 gift card, £50 gift card or £100
gift card.
• Displays how much was deducted from the gift card
• Display how much money remains in the gift card.
Paste your code below:
27. The formula to calculate the file size of a sound file is:
File size (bits) = Sampling rate (Hz) × Resolution (bits) × Length of sample (seconds)
Create an algorithm that:
• Asks the user to input the sample rate.
• Asks the user to input the resolution.
• Asks the user to input the length of the sample in minutes.
• Calculates and stores the file size in a variable called “size”.
• Displays the file size (in bits) in a full sentence.
Paste your code below:

https://youtu.be/pMzSuBpQZkU
28. Tom wants to hire a car. He wants to know how much it will cost him to drive to a
different city and back.
The hiring company charges the following:
• A booking fee of £5
• First mile costs = £2
• Rest of the miles cost: 0.25 per mile.
Tom also needs to fill the car with Diesel, the car uses approximately £0.5 in diesel per mile.
Create an algorithm that:
• Asks the user how many miles are they going to travel.
• Calculates and displays the total cost of the journey.
Paste your code below:

https://youtu.be/DBqLDaa3530
29. Create a program that calculates the area of a circle.
Area of circle formula is PI*R^2 so: PI * RADIUS * RADIUS
Your algorithm must:
• Ask the user for the radius
• Create a variable called pi and set it to 3.14
• Calculates and displays the area of the circle in a full sentence.
Paste your code below:

https://youtu.be/5Gi3df3VcTM
30. Create a program that calculates the area of a triangle.
Area of triangle formula: (base* height) / 2
Your algorithm must:
• Ask the user for the base
• Ask the user for the height
• Create a variable called area and stores the area of the triangle.
• Displays the area of the triangle in a full sentence.
Paste your code below:

https://youtu.be/jT1Txe1FJ2o
Homework

https://youtu.be/B7Drdhyz8KE
Watch the video
Revisit phase / Starter
1. State the values of a, b, c, d and e after
the following operations are carried out:
1
(a) a = 26 MOD 5 =
7
(b) b = 50 DIV 7 =
1000
(c) c = 10 ^ 3 =
0
(d) d = 10 MOD 5 =
456
(e) e = “4” + “56” =
Lesson 4 Sunday, June 30, 2024

If statement
Lesson objectives…
Identify the meaning of basic
comparison operators.
Identify the purpose of selection
Prepare a python program using If
statements.
Lesson theory

https://youtu.be/B7Drdhyz8KE
Comparison Operators
Operator Meaning Example True/False
> Greater than 44 > 5
True
< Less than 33<20
False
== Equal to 10==20 False
!= Not equal to 5 != 2
True
>= Greater than OR 20>=20
equal to True
<= Less than OR 5<=10
equal to True
Consolidation phase- Your turn
Operator Meaning Example True/False
> Greater than 2>5
False
< Less than 23<20
False
== Equal to 16==16 True
!= Not equal to 7 != 7
False
>= Greater than OR 11>=20
equal to False
<= Less than OR 30<=30
equal to True
Application phase – Apply what you have learnt!

Go on: www.kahoot.it

https://create.kahoot.it/share/comparison-
operators-python/4c284ca9-fd7a-4524-8753
-b08c6896d230

Game pin will be on the board.


If statement

If Then

Else
Structure of an if statement
Asks the user to input a
Variable name

name = input("Enter a name")


if name == "Rose":
print("Hi Rose")
After writing if, This space (TAB) represents A condition
Indentation which signifies must end with a
you need a
the start and end of a block COLON :
condition. of code.
Structure of an if statement

name = input("Enter a name")


if name == "Rose":
print("Hi Rose")
Must end
else: with a
print("Who are you") colon

Must be
indented.
Means OTHERWISE. It will display Who are
you If anything other than Rose is entered.
Structure of an if statement
name = input("Enter a name")
if name == "Rose": Remember to always
print("Hi Rose") put
" Quotation marks"
elif name == "Sarah": around strings.
print("Hi Sarah")
else:
print("Who are you")
Elif stands for ELSE IF. You can have as many elif as you
want in a single if statement, however you can only
have 1 if and 1 else.
Structure of an if statement
password = input("enter a password")
if password == "Over123":
print("Access Granted")
elif password == "Overwatch":
print("close")
else:
print("Access Denied")
What will this program display if the user enters
Overwatch as the password? Close
Structure of an if statement
password = input("enter a password")
if password == "Over123":
print("Access Granted")
elif password == "Overwatch":
print("close")
else:
print("Access Denied")
What will this program display if the user enters
Over123 as the password? Access Granted
Structure of an if statement
password = input("enter a password")
if password == "Over123":
print("Access Granted")
elif password == "Overwatch":
print("close")
else:
print("Access Denied")
What will this program display if the user enters
Potatos123 as the password? Access Denied
Structure of an if statement
powerlevel= int(input("enter power level"))
if powerlevel > 9000:
Note: DO NOT PUT
print("Over 9000") "QUOTATION MARKS"
elif powerlevel < 9000: AROUND NUMBERS.
print("Less than 9000")
else:
print("It is 9000")

What will this program display if the user enters 9500 as


the power level? Over 9000
Application phase – Apply your knowledge

Complete task 31-43


Task 31 – State whether the following statements are True or False
Question True/False
5 == 3
2 != 5
5>6
8<2
2!=2
7==7
1>=1
7>=2
9<=2
9<=9

https://youtu.be/RUaClzQ_2lQ
32. Create a program that:
• Asks for the user’s age.
• If the age is 18 or above, display “You are old enough to vote”
• Otherwise display “You are not old enough to vote”.
Paste your code below:

https://youtu.be/fr2fBeV8ZYQ
33. Create a program that:
• Asks for the user’s name.
• If the user enters Nina as the name, display “Welcome Nina”,
• Otherwise display “Hello stranger”.
Paste your code below:

https://youtu.be/jgnVkem7RJo
34. Create a program that:
• Greets the user by displaying “Welcome user” on the screen.
• Asks the user for favourite singer.
• If the user enters “Beyonce”, Display “Good singer”
• If the user enters “Ed”, display “Pretty decent”
• Displays “Not too bad” for any other entry
Paste your code below:

https://youtu.be/hH67Yy3Z9C8
35. Create a program that:
• Ask user to enter a grade.
• If the grade is 90 or above, display “Grade 9 / A**”
• if grade 80 or above, display “Grade 8 / A*”
• if grade 70 or above, display “Grade 7 / A”
• Otherwise, display “Pass”.
Paste your code below:

https://youtu.be/Zuzf7Xu9Rts
36. Create a program that:
• Ask the user the following: “Is there a lockdown?”
• If the user types “No” as the answer, display “Have a good day”
• Otherwise ask them if they are at home. If they they type “yes” as the answer, display
“Well done”, otherwise display “Go home”.
Hint: Use nested if statement (an if statement inside another if statement)
Paste your code below:

https://youtu.be/jLLJSFzrbhc
37. Create a program that:
• Ask the user to input 2 numbers.
• Ask the user if they want to multiply the numbers together.
• If the user enters “Yes”, multiply the 2 numbers together and display the answer.
• Otherwise display “Ok boss”.
Paste your code below:

https://youtu.be/SqJgRMgnBEw
38) Create a program that:
• Asks the user to enter a number.
• If the number is larger than 100, display “too large”.
• Otherwise display “too small”.
Paste your code below:

https://youtu.be/63KCotb2Tzk
39) Create a program that:
• Asks the user to enter a football team name.
• If the user enters Chelsea, display blue.
• If user enters Liverpool, display red.
• Otherwise display team not registered.
Paste your code below:

https://youtu.be/Y8CVJTCX-Ms
40) Create a program that:
• Asks the user to enter 2 numbers.
• If the first number is bigger than 10, add the two numbers.
• Otherwise multiply the two numbers.
• Display the result.
Paste your code below:

https://youtu.be/Mp7H6nbwc9k
41) A manager of an amusement park wants to create an algorithm for a new discount. A
ticket costs £20. There is a 50% discount for people who hold season tickets and 25% for
people without a season ticket.
Your algorithm must:
• Ask the user if they have a season ticket or not.
• Display the final price after the discount.
Paste your code below:

https://youtu.be/WUpNYcTTo1A
42) Create a program that:
• Ask the user to enter traffic light colour.
• If colour is = red, display STOP.
• If colour = yellow display get ready.
• If colour is = green, display GO.
• Otherwise display an “Error”.
Paste your code below:

https://youtu.be/a3dHnA-meBg
43) Create a program that:
• Asks the user to enter two numbers.
• Ask them if they want the numbers added or multiplied.
• If the user enters add, add the 2 numbers together then display it on the screen.
• If the user enters multiply, multiply the 2 numbers together then display it on the
screen.
• Otherwise display “Wrong answer”.
Paste your code below:

https://youtu.be/o8IuQLPlAfE
Homework
Watch the video:
https://youtu.be/EAnQSGmqNc8
Revisit phase - Starter

age = int(input("Enter your age"))


if age>=18:
print("You are old enough to vote")
elif age==17:
print("You can vote in 1 year")
else:
print("You are still too young")
What will the following program display? If the user enters 17?

What will the following program display? If the user enters 19?
What will the following program display? If the user enters 14?
Lesson 5 Sunday, June 30, 2024

If statement part 2
Lesson objectives…
Use IF statement to solve real life
problems.
Understand the purpose of Boolean
operators.
Use Boolean operators to create
algorithms.
Lesson theory video
Watch the video:
https://youtu.be/EAnQSGmqNc8
Knowledge phase

Boolean operators can be used to make


selection statements more efficient and
versatile. We use AND, OR & NOT.

It’s important to use brackets in long


Boolean expressions.

Order: Brackets, NOT, AND then OR.


AND OPERATOR

The AND operator will output True if both


Boolean expression True/False Reason
sides are true, otherwise it will be False.
For example:
5 > 2 AND 8 < 12 True Both sides are True.

11 > 5 AND 7 == 3 False Right side is False, so


overall statement
will be False
OR OPERATOR

The OR operator will output True if either or


both
Boolean sides are
expression true, otherwise
True/False it will be
Reason
False.
10 == 10 OR 7 < 12 For example: Both sides are True.
True
11 > 5 OR 5 == 3 True Left side is True, so
overall statement
will be True
10 == 5 OR 11> 20 False Both sides are False
NOT OPERATOR

The NOT operator reverses the statement, if


it’s
Boolean
True it become False,
True/False
if it’s False
Reason
it
expression becomes True.
NOT (20== 20)
For
False
example: 20== 20 is True, so
reverse of true is False
NOT (10 < 5) True 10 < 5 is False, so reverse
of False is True

NOT (25> 20) False 25 > 20 is True, so reverse


of true is False
If statement

Boolean expression True/False


11 > 5 AND 7 == 3 False
NOT(13 == 2) True
11 <= 2 OR 8 != 5 True
NOT(12 > 2 AND 5 < 1) True
If statement
n = int(input("Enter a number"))
if n == 2 or n == 3:
print("Correct")
else:
print("Incorrect")
What will this program display if the user enters
10 as the number? Incorrect
What will this program display if the user enters
2 as the number? Correct
Consolidation
Go on
www.kahoot.it

Game pin will be on the board.

https://play.kahoot.it/#/k/20e5c6d0-9106-4711-88a1-ee
240367208a
Example:

name = input(“What is your name?”)


if name == "James" or name == "Don":
print("I know you!")
else:
print("Who
What will this program are you?")
display if the user enters
James as the name? I know you!
What will this program display if the user enters
Bradley as the name? Who are you?
Application phase

Complete task 44- 49


Then complete any unfinished tasks from previous
lessons.
44) Create a program that:
• Asks the user to enter a username and password.
• If they get the username AND password are correct, display a “logged in” Otherwise,
display “Incorrect details”.
• Assume correct username= “RushB” and correct password = “CSGO”
Paste your code below:

https://youtu.be/JXVO1r-tMSQ
45) Create a program that:
• Ask the user if they play games on pc.
• Ask them if they play on console.
• if pc = yes and console = yes, display Game master
• if pc = yes and console = no, display pc master
• if pc = no and console = yes, display console master
• Display “Error” for any other inputs.
Paste your code below:

https://youtu.be/0II-TPzrnsg
46) Create a program that:
• Asks for the user’s age.
• If the age is between 13 and 19, display “You are a teenager”.
• If the age is 11 or 12, display “You are a tween.”
• Otherwise display “Invalid age”.
Paste your code below:

https://youtu.be/iS6fkWBgd-U
47) Create a program that:
• Asks the user for current temperature.
• Asks the user if it’s raining outside.
• If temperature is less than 13 and it’s raining, display “Wear a coat and bring an
umbrella”.
• If temperature is less than 13 degrees and it’s not raining, display “Wear a coat”.
• If temperature is 13 or above, and it’s raining, display bring an umbrella.
• Otherwise display “you don’t need a coat or an umbrella”.
Paste your code below:

https://youtu.be/TAwA7_KPsaU
48) Write a program that:
• Asks the user for a temperature
• If the temperature is greater than 28, display “T Shirt weather”.
• If the temperature is between 18 and 28, display “Lovely weather”
• If the temperature is less than 18, output “It’s cold”
Paste your code on the below:

https://youtu.be/PlqMEbboAh0
49) Create a program that allows the user to convert between different units:
• Ask the user for a number.
• Ask the user what unit is the data currently in.
• Ask the user what unit do they want to convert to.
• Convert between Bits, Bytes and Kilobytes.
There are 8 bits in 1 bye, and 1000 bytes in 1 Kilobytes.
Paste your code on the below:

https://youtu.be/nzQF_w3pwIQ
Homework
Watch the video:
https://youtu.be/dEli50rqryw
Lesson 6 Sunday, June 30, 2024

If statement part 3
Lesson objectives…
Explain what is meant by syntax and
logic errors.
Identify syntax and logic errors in
code.
Prepare a python program using
modulo to distinguish between odd
and even numbers.
Lesson theory
Watch the video:
https://youtu.be/dEli50rqryw
Syntax error
Syntax Error: When you break the rules of the
programming language.
Examples:

• Iff name == “hello”:

• if name != “hello”

• else::
Logic Error
Logic Error: When your program runs however it
produces an unexpected results.
Examples:

• Using incorrect comparison operators such as >


instead of < .
• Using incorrect arthmetic operators such as -
instead of + .
• Using incorrect boolean operators such as
“AND” instead of “OR”
Logic Error
Logic error:
must be >
Syntax
instead of <
error:
Double
Brackets
Logic error: missing ))
must be <
instead of >

Syntax error: Missing Quotation


mark "

Spot the syntax and Logic errors in the code


above:
Modulus
We can use Modulus to find if a number is odd or even.
Example:
10 % 2 = 0
6%2=0
100 % 2 = 0
8%2=0
98 % 2 = 0

As you can see, you can use modulo 2 to find if the


number is even.
If the remainder is 0, then it’s even, if the remainder is 1,
then its odd.
Tasks
Complete task 50 – 57
Then complete any unfinished tasks from previous
lessons.
50. Ask the user for a number.
Display if the number is odd or even.
Hint: Use MODULO %
Paste your code below:

https://youtu.be/8-XrL1lTiag
51) The following formula is used to convert Celsius to Fahrenheit: Y°F=(X°C × 9/5) + 32
The following formula is used to convert Fahrenheit to Celsius: X°C=(Y°F − 32) × 5/9
represents the value in Celsius and Y represent the value in Fahrenheit.
Use the formula above to create an algorithm that converts Celsius to Fahrenheit.
Your algorithm must:
• Ask the user whether they want to convert from (Celsius to Fahrenheit) or (Fahrenheit to
Celsius)
• Ask the user for the temperature.
• Converts the temperature into what the user picked.
• Displays the answer in a full sentence.
Paste your code below:

https://youtu.be/XSjtqaM6Zsk
52) Create a program that:
• Asks the user if it’s August or not.
• If they answer yes, display “Yay summer holiday”
• Otherwise ask them if it’s the weekend.
• If the answer is yes, display “Yay no school”.
• Otherwise display, “Bad luck, you have school today”.
• Use nested if statement (An if statement inside another if statement”)
Paste your code below:

https://youtu.be/S0-JoBWj_o4
53) Create a program that:
• Asks the user enter today’s day (Monday, Tuesday etc.…)
• Displays “Yay it’s the weekend” if the user enters Saturday or Sunday.
• Displays how many days left until the weekend. (Example: “5 days until the weekend if
the user enters Monday”)
• Use nested if statement (if statement inside another if statement)
Paste your code below:

https://youtu.be/dohoiIl3Y7w
54) When a character takes damage in a game, their health bar goes down.
The health bar starts to increase (regenerate) 5 seconds from the last time a player was hit and by 1% per
second. At 100%, the health bar has 500 health points.
Create an algorithm that:
• Asks the user how many health points remained after they
received the last attack (out of 500)
• Asks them how many seconds ago did they get hit.
• Calculates the user’s current health points out of 500.
• Calculates the user’s health point as a percentage % out of 100%.
• The current health points must NOT be above 500.
Paste your code below:

https://youtu.be/dGgpqzmpUc0
55) Code the following flowchart in python:

https://youtu.be/aCS3H-grjHo
Paste your code below:

No

Yes

No
56) Code the following flowchart in python:

https://youtu.be/a0aMxxBg
0XA

Paste your code below:


57) Tom wants to hire a car. He wants to know how much it will cost him to drive to a different city and back.
• The hiring company charges the following:
• A booking fee of £5
• There is no booking fee for customers who own a membership card.
• First mile costs = £2
• Rest of the miles cost: 0.25 per mile.
• Tom also needs to fill the car with Diesel, the car uses approximately £0.5 in diesel per mile.
Create an algorithm that:
• Asks the user how many miles are they going to travel.
• Asks the user if they have a membership card.
• Calculates the total cost of the journey. (remember to deduct the booking fee if they have a membership card)
Paste your code below:

https://youtu.be/XzpSiBTH0As
End of unit test
MS Form assessment:
Test link:
https://forms.office.com/Pages/ResponsePage.aspx?id=
NRrmRxWbmk-P3e1XFrwgvUKZUnHi_FpMtvQIsO11-sxU
NDAzTTRMQkdMUjFPSkJWOU82MzRGVDc5Sy4u
Revisit phase - Starter

Boolean expression True/False


4 > 5 AND 7 == 7 False
NOT(2 == 2) False
11 <= 2 OR 4 != 1 True
NOT(12 > 2 OR 5 < 1) False
5 != 5 AND 7 > 2 False
10 >= 10 and 5<=6 True
Lesson 6 Sunday, June 30, 2024

String Manipulation
Lesson objectives…
 Be able to manipulate strings.
 Apply string manipulations to
different python tasks.
Understand the purpose of substrings
and length checks.
Video
Lesson theory video: https://youtu.be/gEJMBkArksg
String manipulation - Concatenation
Concatenation means ‘joining’.

The ‘+’ sign joins together two or more string (without a


space).

firstname = input("Enter your firstname")


surname = input("Enter your surname")
print(firstname+surname)
String manipulation - Concatenation
As you saw on the previous slide the “+” joined two strings
together, however it did not add any space to it. If we wanted
to join things together with a space, we use comma “,”

firstname = input("Enter your firstname")


surname = input("Enter your surname")
print(firstname, surname)
Why not just use a comma all the time?

firstname = input("Enter your firstname")


surname = input(firstname, " Enter your surname")

The above code will give you an error, because you can’t add a
comma when storing a value in a variable because it will
count it as an argument. However you can concatenate using
“+” sign.
Correct way:

firstname = input("Enter your firstname")


surname = input("hey " +firstname+" Enter your surname")
String manipulation - Length

This example program asks the user to input a


name. It then finds the length of the string and
stores it in the nameLength variable. The length of
the string is displayed.
String manipulation - Length
len() is a python function that finds the length
of a string.
Example 1:
name = "hello"
print( len (name) )
The above program will diplay the number 5. Because
that’s how many characters is in the string "hello"
String manipulation - Length
len() is a python function that finds the length
of a string.
Example 2:
name = input("Enter a name")
print( len (name) )
The above program will diplay the number 4 if the user
types Jeff as the name.
Tasks
Complete task 58 – 61
Then complete any unfinished tasks from previous
lessons.
58. Create a program that: https://youtu.be/pfKyPyVMFZ4
• Asks the user for firstname.
• Asks the user for surname (use the firstname inside the question – example below)
• Display the firstname and surname in a full sentence.
• Use + sign to concatenate.
Paste your code below:
59. Create a program that:
• Asks the user for their favourite drink.
• Store the length of the string in a variable called “drinkLength”
• Display the length of the drink on the screen in a full sentence.
Paste your code below:

https://youtu.be/I6KwqKJGt9w
60. Create a program that:
• Asks the user for their firstname.
• Ask the user for their surname
• Concatenate the two strings(variables) together without space and store it in a variable
called “total”.
• Display the total length of firstname+surname.
Paste your code below:

https://youtu.be/E7qQWQUKnEc
61. Create a program that:
• Ask the user to enter a colour that contains 6 letters.
• If the user enters the correct answer. Display “well done”
• Otherwise display “incorrect”.
Hint: len(colour) https://youtu.be/oqjj0sFaKpg
Paste your code below:
String manipulation – Case conversion

You can turn your string to either


uppercase using .upper() or lowercase
using .lower()
name = input("Enter your name")
print ( name.upper() )
print ( name.lower() )
String manipulation – Substring
Substrings are used to extract part of a string.
Index numbers starts from 0.
What do you think the following code will display?
country = "ENGLAND"
print(country[0])

It will display the letter “E”

Index 0 1 2 3 4 5 6
number
Characters E N G L A N D
String manipulation – Substring
-1 is used to select the last character of a
string.
What do you think the following code will display?
country = "ENGLAND"
print(country[-1])

It will display the letter “D”

Index 0 1 2 3 4 5 6
number
Characters E N G L A N D
String manipulation – Substring – PYTHON
The following code will display: “EN”

country = "ENGLAND"
print( country [ 0 : 2 ] )

Start position
Up to but not included

Index 0 1 2 3 4 5 6
number
Characters E N G L A N D
Substring in the exam (pseudocode)
The following code will display: " LAND "
country = "ENGLAND"
print( country.substring(3,4) )
How many letters it will
Start position display

• Substrings start from 0. So (3,4) means it starts with


the letter L “index 3”, it displays L and 3 more letters
for a total of 4 letters.
Index 0 1 2 3 4 5 6
number
Characters E N G L A N D
Substring in the exam (pseudocode)
The following code will display: " GLA "
country = "ENGLAND"
print( country.substring(2,3) )
How many letters it will
Start position display

• (2,3) means it starts with the letter G “index 2”, it


displays L and 2 more letters for a total of 3 letters.

Index 0 1 2 3 4 5 6
number
Characters E N G L A N D
Substring in the exam (pseudocode)
What will the following code display?
country = “United States"
print( country.substring (1,3) )
How many letters it will
Start position display

It will display “nit”


Index 0 1 2 3 4 5 6 7 8 9 10 11 12
number

Character U n i t e d S t a t e s
Substring in the exam (pseudocode)
What will the following code display?
country = “United States"
print( country.substring (3,3) )
How many letters it will
Start position display

It will display “ted”


Index 0 1 2 3 4 5 6 7 8 9 10 11 12
number

Character U n i t e d S t a t e s
Substring in the exam (pseudocode)
What will the following code display?
country = “United States"
print( country.substring(7,6) )
How many letters it will
Start position display

It will display “States”


Tasks
Complete task 62 – 71
Then complete any unfinished tasks from previous
lessons.
62. Create a program that:
• Ask the user for their firstname.
• Ask the user for their surname.
• Display the first letter of the user’s firstname.
• On a separate line, display the first 4 letters of the user’s firstname.
• On another line, display the last letter of the user’s surname.
Paste your code below:

https://youtu.be/YSxb00uGxvU
63. Create a program that:
• Ask the user for a quote.
• Display the quote in uppercase
• Then displays the quote in lowercase on a separate line.
Paste your code below:

https://youtu.be/dY_BD6PA90E
64. Create a program that:
• Ask the user for their name.
• Ask the user if they want their name to be displayed in capital letters or lowercase.
• If the user types lowercase, display the name in lowercase.
• If the user types uppercase, display the name in uppercase.
• Otherwise display “wrong choice”.
Paste your code below:

https://youtu.be/vehulrRbpOU
Formatting text

\n - This adds a new line. Example:


print("Hello \nWorld") would display:

\t - The t stands for “tab” which adds extra


space. Example:
print("Hello \tWorld") would display:
65) Create a program that:
• Displays “Good” then “Game” on 2 separate lines using \n and 1 print command.
• On another line, display Good game but with 4 spaces (TAB) in the middle using \t.
Paste your code below:

https://youtu.be/CN3DmEwr4RA
66. Create a program that: https://youtu.be/zdlbAFV846o
• Ask the user for a school name.
• Ask the if they want to see the first or last letter of the school name.
• If the answer is “first”, display the first letter of the school name
• If the answer is “last”, display the last letter of the school name “hint: use -1 to display
last letter”.
• Otherwise display “wrong option”.
Paste your code below:
67. Create a program that:
• Asks the user for a song name.
• Asks the user how many characters do they want to see from the song name starting
from the first character. Example: If the user types 4, show the first 4 characters of the
song name.
• Display the correct amount of characters depending on the user’s answer.
• Store the total length of the song name in a variable called: “songLength”
• On a separate line, display the total length of the song name in a full sentence.
Paste your code below:

https://youtu.be/dF5DSpuGNMA
68. Create a program that:
• Asks the user for the name of their favourite singer.
• Store the length of the singer’s name in a variable called nameLength.
• Asks the user how many characters do they want to see from the singer’s name starting from the
first character. Example: If the user types 4, show the first 4 characters of the singer’s name.
• Add validation check so that the amount of characters the user enters is not bigger than the total
length of the name. For example: If the total length of the name is 7 characters, and the user enters
9 when asked how many characters do they want to see, then display an “Error”. Otherwise display
the correct number of characters.
Paste your code below:

https://youtu.be/xkq7Vf6Zidc
69. Create a program that:
• Asks the user for their name.
• Extract the first character of their name and save it in a variable called “first”.
• Force the first character to upper case using .upper() function.
• Use concatenation to join the first letter back to the original string.
• Example: if the user types suffar as there name, you need to display: Suffar
Paste your code below:

https://youtu.be/brTg1MwfVGk
70. Create a program that:
• Asks the user to enter a school name.
• Asks the user if they want the school name to be displayed in capital letters or lower
case.
• If the answer is “uppercase”, display the school name in uppercase.
• if the answer is “lowercase”, display the school name in lowercase
• Otherwise display “Wrong option”.
Paste your code below:

https://youtu.be/Ye8YK7kltm4
71. Identify what each of the following code will
display if quote = “Legendary” https://youtu.be/Mh79NhBK9f4
Code Output
print( quote.upper() )

print( quote.lower() )

print( len (quote) )


print( quote.substring(0) )
print( quote.substring(0,3) )
print( quote.substring(2,3) )
print( quote.substring(5,4) )
Homework: https://youtu.be/myTwo-B9E4k
Revisit phase - Starter
Used to … Function / symbol

Change the string to .lower()


lowercase

Change the string to


uppercase .upper()
Symbol used to
concatenate 2 strings. +
Finds the length of a
string len()
Used to extract part of a
string in PSEUDOCODE:
.substring()
Lesson 9 Sunday, June 30, 2024

Iteration
Lesson objectives…
Understand the structure of a for loop.
Solve real-world problems using for loops.
Understand how to use selection and
iteration on a single program to solve a
problem.
Iteration theory: https://youtu.be/myTwo-B9E4k
Knowledge phase

Iteration: The repetition of a process/


section of code.
Structure of a for loop
Variable Number of iteration End with a colon

for __ in range(__) :
code to be run here
more code to be run here
this code not in the for loop

Note that indenting (using the TAB key) is used to


decide which code goes inside the loop.
Number of iteration
Variable

for x in range (5) :


print("Hello")
print("The end")

It will display “hello” 5 times then it will


display “The end” ONCE.
Number of iteration
Variable

for y in range (3) :


name=input("Enter a name")
print("Your name is", name)
print(“Good bye")

It will ask the question 3 times and then displays the


names 3 times then it will display “Good bye” once
Start from
Variable
Up to but not included

for x in range (1,11) :


print(x)

This is used if you want to use a counter. x is a variable


that will hold numbers from 1 to 10.
Start from
Variable
Up to but not included

for count in range (1,6) :


print(count* 10)

This will produce the 10 times table from 1 to 5.


number = int(input("Enter number"))
for i in range (number) :
print("hello")

This will display “hello” depending on the number that the


user entered.
number = int ( input ( "Enter number " ) )
for count in range (1,6) :
print ( number * count )

What will the above program display if the user enters


100 as the number.
• It will display the times table of the number that the
user enters (100) from 1 to 5.
Tasks
Complete task 72 – 86
Then complete any unfinished tasks from previous
lessons.
72. https://youtu.be/m4feUCbSbIU
• Copy the code below and run it.
• Then change the % to a "$".
• Run it again then comment on the code to explain the purpose of the code.

for count in range(1,31):


print (count * "%")

Paste your new code below with comments:


73. Use a for loop to display “welcome” 100 times. Then display “bye” ONCE.
Paste your code below:

https://youtu.be/ptQlhaUF8g8
74. Create a program that counts from 1 to 100.
Paste your code below:

https://youtu.be/CJxCX91EguY
75. Ask the user for a number. Display the times table of that number from 1 to 100.
Paste your code below:

https://youtu.be/h34JCAGAujU
76. Ask the user for a number. Repeat ‘I like turtles’ a certain number of times, based on a
number entered by the user. (If the user enters 5, display I like turtles 5 times)
Paste your code below:

https://youtu.be/S8vzq2pTXLM
77. Ask the user to enter a name. Ask the user how many times they want their name to be
displayed. Display the user’s name repeatedly depending on the user’s answer.
Paste your code below:

https://youtu.be/2liNPIqBOcA
78. Ask the user for a number. Display the times table of that number from 1 to 5 in the
following style               
Paste your code below:

https://youtu.be/5Tu8qHI-iB8
79) Ask the user their name and their age. Print their name the number of times as their
age.
Paste your code below:

https://youtu.be/6jMDtCSWf5c
80) Write a program that:
• Asks the user to enter a number
• Adds 5 to that number
• Displays the new number on the screen.
• Make the above process repeat 6 times.
Paste your code below:

https://youtu.be/sDr1Wo1ndEI
81. Create a program that:
• Asks the user for a name.
• Display the user’s name as many times as the letters in the name. For example: If the
name has 7 letters, display that name 7 times.
Paste your code below:

https://youtu.be/CDFqJHuWEjE
82) Create a program that:
• Asks “how many negatives did you get?”
• If the answer is 1, display prompt 10 times.
• if the answer is 2, display reminder 50 times.
• If the answer is 3, display warning 100 times
• Display removal 500 times if any other number is entered.
Paste your code below:

https://youtu.be/_3DIOrsf77A
83) Wzzap market is a shop that sells face masks.
Create a program that:
• Asks if the shop is open.
• If the user answers "yes", display "Can I get a face mask please" 5 times using a for
loop.
• Otherwise display “I need to stay at home!” 100 times using a for loop.
Paste your code below:

https://youtu.be/Zoh_TZNxbXQ
84) Create a program that: https://youtu.be/qMDj9pqkEck
• Asks for the user’s age.
• If the age is greater than 12, display happy birthday 100 times.
• Otherwise display happy birthday 12 times.
Paste your code below:
85) Create a program that:
• Displays all odd numbers between 1 and 99.
Paste your code below:

https://youtu.be/l4gS8fdv1Ho
86) Create a program that:
• Displays all even numbers between 1 and 100.
Paste your code below:

https://youtu.be/yVAmfSIrges
Homework: https://youtu.be/LDjUhj7yvkQ
Revisit phase - Starter
What will the following code display?

Start from
Variable
Up to but not included

for count in range (1,6) :


print(count* 10)

This will produce the times table of 10 from 1 to 5.


Revisit phase - Starter
What will the following code display?

Start from
Variable
Up to but not included

for count in range (1,101) :


print(count* 3)

• This will produce the 3 times table from 1 to 100.


Lesson 10 Sunday, June 30, 2024

Iteration & Trace table


Lesson objectives…
Understand the purpose of a trace table.
Complete trace tables for a given scenario
Solve real world problems using iteration.
Theory: https://youtu.be/LDjUhj7yvkQ
Trace table
Trace table is used to:
• Find the output of programs
• Find the changes in the values of variables
• Find errors in the program.
Example of a simple trace table:

x Output
for x in range (1,6):
print(x*2) 1 2
2 4
3 6
4 8
5 10
Example of a trace table:

x x<3 Output

for x in range (1,5):


if x < 3: 1 True Less than 3
print("less than 3")
else: 2 True Less than 3
print("3 or above")
3 False 3 or above

4 False 3 or above
Assume the user enters the following values: “Sam”,
“upper”, “Ayo”, “lower”, “Zoe”,”upper”
x choice case Output
for x in range(1,4):
choice = input("Enter a word")
case = input("upper or lower")
if case == "upper":
print(choice.upper () ) 1 Sam upper SAM
else:
print(choice.lower () )

2 Ayo lower ayo

3 Zoe upper ZOE


Tasks
Complete task 87 – 102
Then complete any unfinished tasks from previous
lessons.
87. Complete the trace table.
https://youtu.be/IcX4DMxqAKY

X Output
for x in range (1,6):
print(x**2)
88. Complete the trace table. https://youtu.be/_9-cXSiAoSw

X country Output
for x in range (1,5):
country = "France"
print(country[x])
89. Assume the user enters the following values: 0, 3, 2, 5

x Is x < 3 num Output


for x in range (2,7):
if x < 3:
print("it’s Jeff")
else:
num = int(input("Enter a number"))
print(x * num)

https://youtu.be/TNKu5Z96Xrc
90. Assume the user enters the following values: “Nick”,
“Lukas”, “Lara”, “Tim”
X name length Output
for x in range (1,5):
name= input("Enter a
name")
length = len(name)
print(x * length)
https://youtu.be/kT69M2pO9IY
91. Assume the user enters the following values: 7, 0, 4, 8
X country num letter Output

for x in range (1,5):


country = "United States"
num = int(input("Enter a number"))
letter = country[num]
print(country[x] + letter)

https://youtu.be/IleEze5i0YE
92. Assume the user enters the following values: 4, *, 3, % ,6, %, 5, *
Hint: % = MOD (finding the remainder)
x num operato Output
r

for x in range (1,5):


num = int(input("Enter a number"))
operator = input("Enter operator")
if operator == "%":
print(num % x)
else:
print(num*x)

https://youtu.be/VyrDzifn_s4
93. Complete the trace table
x y Output

for x in range (1,4):


for y in range (1,4):
print( x * y )
https://youtu.be/lEzhzWsoBds
94. Complete the trace table.
X Is x % 2 == 0 Output

for x in range (1,7):


if x % 2 == 0:
print(x)
https://youtu.be/GPPdK7qyWCA
95. Create a nested loop (for loop inside a for loop) to display the multiplication timetable
between 1 and 5. Display your answer in an appropriate format. Example:
Paste your code below:

https://youtu.be/QhnSLxiLYRY
96. A teacher is recording the amount of time it take to solve a Rubik cube. The algorithm
must:
• Asks the user to input the number of attempts.
• Ask the user to input how long it took in seconds to solve the Rubik cube.
• Displays the total time of all attempts.
• Displays the average time of the attempts.
Paste your code below:

https://youtu.be/S6BmzSalQh4
97. Code the following flowchart in python:

https://youtu.be/74Iy-yS5DoE

Paste your code below:


98. Code the following flowchart in python:

Paste your code below: https://youtu.be/G1PqTSjg9G4


99. Create a program to calculate how many points a player has after 5 games. Players receive 3 points for winning, 1
point for a draw and 0 points for losing a game. The player will move up to a higher division if they achieve at least 12
points. The player will move to a lower division if they achieve a less than 6 points. Otherwise they will stay in the
same division. Your program must:
• Ask the player if they have won, lost or drew.
• Calculate the total number of points earned after 5 games.
• Output the number of points earned after 5 games.
• Use selection to find out if the player has been promoted, relegated or stayed in the same division.
• Output the answer with an appropriate message.
Paste your code below:

https://youtu.be/rRZUXiBA254
100. Create a program that:
• Asks the user to input 5 numbers.
• Count and display the following:
• How many of the number are odd
• How many of the numbers are even
• How many of the numbers are positive
• How many of the numbers are negative
• How many of the numbers are zeros.
Paste your code below:

https://youtu.be/eLtas_tvk5c
101. Dodgy Sam visited 6 supermarkets in 1 day to stock pile toilet rolls.
Create an algorithm that:
• Asks the user to input the number of toilet rolls bought at each supermarket.
• If they entered 1 or above, then ask them how much each toilet roll cost.
• Calculate the total amount spent on toilet rolls after visiting all 6 supermarkets.
• Displays the highest amount paid for a single toilet roll.
• Display the lowest amount paid for a single toilet roll.
Hint: Use for a for loop and if statements.
Paste your code below:

https://youtu.be/SbgcFNvQJ44
102. A hospital wants to create a survey to check how many patients are carrying a virus. If the patient has a fever AND
a dry cough, the hospital assumes they test positive for the virus.
Create a program that:
• Ask the user to input how many patients are taking the test.
• Ask the user if they have a fever.
• Ask the user if they have a dry cough.
• If they have both of the above, display you have tested positive for the virus.
• If they tested positive for the virus, ask them if the symptoms are mild or severe.
• Display how many patients tested positive and how many patients tested negative for the virus.
• Display how many patients have mild symptoms and how many patients have severe symptoms
Paste your code below:

https://youtu.be/IGrybPqNnLo
Extension
FOR with different steps

You can change the steps that each loop around will change the
counter value by

for x in range(1, 101, 2) :


print(x)

This will loop around from 1 to 100 counting in steps of 2


FOR with different steps

You can also count down:

for y in range(100,0,-1) :
print(y)

This will count from 100 down to 1.


103. Create a program that counts down from 1000 to 1.
Paste your code below:

https://youtu.be/kY9cIM9PKQk
104. Create a program that counts up in steps of 3 from 1 to 1000.
Paste your code below:

https://youtu.be/IhxP8wLU-LI
Homework: https://youtu.be/gn6RIuipDBw
Revisit phase - Starter
Syntax:
missing
colon
Logic: should be >
instead of <

Syntax: should be
indented. Missing Syntax:
Quotation mark also. missing
parenthesis

Syntax: incorrect
spelling

Identify the syntax and Logic errors in the code


above:
Revisit phase - Starter
What is the definition of:

Syntax error:
• When you break the rules of the programming
language. Example: spelling mistake.

Logic error:
• When your program runs however it produces
an unexpected results.
Lesson 11 Sunday, June 30, 2024

Iteration: While loop


Lesson objectives…
Explain the difference between count
controlled and condition controlled loop.
Solve real life problems using a while loop.
Define the meaning of iteration
Knowledge phase

Theory:
https://youtu.be/gn6RIuipDBw
Knowledge phase

Iteration: The repetition of a process/


section of code.
Count controlled iteration – For loop
An example of a count controlled loop is: For loops
A for loop is count controlled because you know the exact
number of iteration that will occur.
Example:

The code will repeat 10 times.


Condition controlled iteration – While loop
An example of a condition controlled loop is: While loop
A while loop is condition controlled because it will loop
until a condition is met.
Example:

Colon
Condition

Indented which means


It is inside the loop
Structure of a while loop
In a while loop, the process will repeat if the condition is
True.
Condition controlled iteration – While loop
The code below will repeat until the user enters “Suffar”
when asked for a name. Then it will display “You are
correct” once.

Note: Indentation indicates that the block of code is


INSIDE the while loop.
Condition controlled iteration – While loop
To set a counter, you need to create a variable, then add
to that variable whenever you loop. Example below:

This line
increment
total by 1.
Condition controlled iteration – While loop
A counter is important because it can be used in different
ways with a while loop. The program below will ask for a
number and add it to a total until the total is above 100.
Example below:

Adds the number


entered by the user to
a total.
Condition controlled iteration – While loop
You can set variables to either True or False to start a
loop. Example below:

Breaks the
while loop.
Tasks
Complete task 105 – 114
Then complete any unfinished tasks from previous
lessons.
105. Create a program that:
• Asks the user to input a password.
• If password is correct, display correct password.
• If password is incorrect, display incorrect password.
• The program will repeatedly ask the same question until the correct answer is entered.
• Assume the correct password is: “csgo1234”
Paste your code below:

https://youtu.be/ZnUOqSNizJ4
106. Create a program that:
• Asks the user to enter 2 numbers that are the equal to each other.
• Display the first number multiplied by the second number if they are both the same.
• The program will repeatedly ask the user for 2 numbers until both numbers are equal
to each other.
Paste your code on the next slide:

https://youtu.be/xjuCDg6zqt0
107. Use a while loop to create the 2 times table up to 100. Display “You have finished”
when you get to the number 100.
Paste your code below:

https://youtu.be/AotoYo5HfKI
108. Display “Hey” and “Bye” on separate lines 1000 times using a while loop.
Paste your code below:

https://youtu.be/3NjSTFSNALk
109. Create a program that:
• Asks the user to input 4 names.
• Displays “You have entered 4 names” when all 4 names has been entered.
• Use a while loop to repeat the question 4 times to enter 4 names in total.
Paste your code below:

https://youtu.be/8oqn709M2b0
110) Create a program that:
• Asks the user to enter a number.
• Adds that number to a total which starts from 0.
• Displays the total.
• The program repeats until the total is over 50.
Paste your code below:

https://youtu.be/4ciL9cQLn0s
111. Code the following flowchart in python:

https://youtu.be/9Hx2vy8Tong

Paste your code below:


112. Create a program that:
• Asks the user for a username.
• If the username is less than 8 characters long, display “username is invalid” and repeat
the question until a valid username is entered.
• Asks the user for a password.
• If the password is less than 8 characters long OR greater than 15 characters, display
“Password is invalid” and repeat the question until a valid password is entered.
Paste your code below:

https://youtu.be/g0ZzxWfTUDc
113. Create a program that:
• Asks the user to input a password.
• If password is correct, display correct password.
• If password is incorrect, display incorrect password.
• Repeat the question until the user enters the correct password or reaches 4 attempts.
After 4 incorrect attempts, display "Locked"
• Assume the password is: "Rashford"
Paste your code below:

https://youtu.be/Ps1Y_b_cQUY
114. Borednite is a new game. When a character takes damage in the game, their health bar goes down.
The health bar starts to increase (regenerate) 5 seconds from the last time a player was hit and by 15%
per second. At 100%, the health bar has 500 health points.
Create an algorithm that:
• Asks the user how many health points they were on since the last time they got attacked.
• Calculates and display how many seconds it will take before the user reaches 100% health (500
points) from the last time they got attacked.
Hint: Use a while loop.
Paste your code below:

https://youtu.be/unyUmU3Kj8U
Homework
Video: https://youtu.be/h40gdOKQkAA
Revisit phase - Starter

Give 1 example of count Give an example of a


controlled loop: condition controlled loop:
• For loop • While loop

What is the purpose of the code What is the purpose of the code
above: above:
• Displays Hello 100 times. • Asks the user to enter a game
repeatedly until the user
enters “Fifa” as the answer
Lesson 12 Sunday, June 30, 2024

While loop & Trace table


Lesson objectives…
Complete trace tables on while loop.
Solve real life problems using a while loop.
Use a if statement and iteration to solve
problems.
Theory
Video: https://youtu.be/h40gdOKQkAA
Example of a simple while loop trace table:

x x<5 Output
x=1
While x < 5: 1 True 2
print(x*2) 2 True 4
x=x+1 3 True 6
print(" The end ")
4 True 8
5 False The end
Assume the user enters the following inputs: 12,10,20,9

num num>=10 Output


num = int(input("Enter number"))
while num >= 10:
print("Incorrect") 12 True Incorrect
num = int(input("Enter number"))
print("Correct")
10 True Incorrect
20 True Incorrect
9 False correct
Assume the user enters the following values:
“yes”, “no”, “chicken”
x dancer x<3 choice output

0 0 True yes great

1 1 True no Why
not?

2 1 True Chicken Why


not?

3
Tasks
Complete task 115 – 124
Then complete any unfinished tasks from previous
lessons.
115. Complete the trace table. Assume the user enters the following values:
“Tom”,”Sarah”,”Andy”,”Ed”

singer singer != "Ed" Output

https://youtu.be/PKkP-fY8M5c
116. Complete the trace table.
x y x<5 x<3 Output

https://youtu.be/vIsF88GPOu4
117. Complete the trace table. Then write the final value of letter:

x name x<4 Output letter


Final value of letter:

https://youtu.be/dcSONzbHWBE
118. Complete the trace table. Assume the user enters the following
numbers: 10, 50, 300,5, 1000
Highest lowest number Output

https://youtu.be/uYylwr_P_ok
119. Complete the trace table. You may not need all the spaces in the
trace table.
x y z total Output
Enter the final value of z:
z=0

https://youtu.be/6TsVTkLdlKY
120. Complete the trace table.

x y z Output

https://youtu.be/HBg2TM2SI2A
121. Complete the trace table.
num num < 5 Output

https://youtu.be/-zVIYOjO62c
122. A league is holding player of the month selection. Create a program that:
• Allow voters to enter either Salah or Vardy.
• Keeps track of how many times each player has been voted for.
• Repeat the process until the user enters “STOP”.
• At the end of the algorithm, output the number of votes for each player.
• Output a message to say whether Salah or Vardy won or if there was a draw.
Paste your code below:

https://youtu.be/PySZjR_lv_k
123. Create a program that:
• Ask the user if they want to enter a number.
• If the answer is not “NO”, ask them to enter an even number.
• If the number entered is odd. Add 1 to it and display the new number.
• Otherwise display the original number entered by the user.
• The process repeats until the user enters “NO”.
Paste your code below:

https://youtu.be/OHFBo8EmBAE
124) A shop owner has a charity box. She wants to create an algorithm that counts how many
customers put money in the charity box. Create an algorithm that:
• Asks the user if the customer donated or not.
• If the customer donated, ask how much did that customer donate.
• Calculate & display how many customers donated, the total amount of donation and how many
customers didn’t donate.
• Your program must repeat until the user enters “end”.
Paste your code below:

https://youtu.be/CzaqxAboT-k
Homework
Video: https://youtu.be/umo-sBtmcaw
Revisit phase – Starter – Data types

Identify 5 different data types used in programming and give an example of


each?
• Integer – Holds hole numbers.
• Example: Age = 20

• Real – Can hold decimal numbers.


• Example: Temperature = 19.5

• Boolean: Holds True or False


• Example: Authorised = True

• Character: Holds a single alphanumeric character.


• Example: Initial = B

• String: Holds alphanumeric characters including numbers that can start with a
0!
• Example: Address: 250 Suffar Road
• Example: phone number: 07002023231
Lesson 13 Sunday, June 30, 2024

While loop & programming constructs


Lesson objectives…
Identify examples of different
programming constructs.
Identify which programming construct is
used in a piece of code.
Solve real life problems using while loop.
Theory
Video: https://youtu.be/umo-sBtmcaw
Programming constructs

There are 3 programming constructs:

Sequence
Selection
Iteration
Sequence
Sequence: The order in which
instructions occur / process.
Example:
Selection
Selection: Determines the path the
program takes.
Example:
Iteration
Iteration: The repetition of a section
of code / process.
Count controlled: Condition controlled
• For loops • While loops
Questions – Cold Call

Which programming construct was used in


the above program?
• Sequence
Questions – Cold Call

Which programming construct was used in


the above program?
• Iteration (Count controlled iteration)
Questions – Cold Call

Which programming construct was used in


the above program?
• Sequence (line 1) & Selection
Questions – Cold Call

Which programming construct was used in


the above program?
• Sequence
Questions – Cold Call

Which programming construct was used in


the above program?
• Sequence (line 1) & Iteration (condition
controlled iteration) line2 to line 3.
Questions – Cold Call

Identify the line numbers where sequence occurred?


• Line 1 and line 2.

Identify the line numbers where selection occurred?


• Line 3 to line 6.
Questions – Cold Call

Identify the line number where iteration starts


• Line 1

Identify the line number where selection starts?


• Line 3
Apply your knowledge:
Log on kahoot:

https://create.kahoot.it/share/y10-python-for-loops-and-out
puts/b386c8e4-ace7-434f-8837-2ef28efccebe
125. A supermarket does not allow customers to buy more than 3 toilet rolls. The
supermarket charges £2 for 1 toilet roll, £6 for 2 toilet rolls, £12 for 3 toilet rolls.
Create an algorithm that:
• Asks the customer how many toilet rolls do they want.
• Calculate the total price depending on how many toilet rolls they buy.
• Asks the question repeatedly if the user enters a number above 3 or a number below 1.
• Outputs the total price.
Paste your code below:

https://youtu.be/gjV7Nl_6qNU
126. Create a program that:
• Asks the user for username and password.
• Checks if both username and passwords are correct.
• Counts and displays the number of attempts it took to enter the username and password
correctly.
• Displays an error message if either the username or password is incorrect.
• The program repeats until the user enters the correct username & password.
• Assume the correct username = "David" and correct password = "abc"
Paste your code below:

https://youtu.be/mqbRE_8V3Ns
127. Create a program for a customer who wants to buy a group ticket to a cinema.
The following restrictions applies to a group ticket:
• Number of tickets must be between 5 and 30.
• Number of children must be between 5 and 25.
• Number of adults must be less than 6.
• Adult ticket cost: £8 and children ticket cost £6.
The program repeats until the customer enters valid inputs for the number of tickets, number of children
and number of adults. Calculate and display the total amount needs to be paid.
Paste your code below:

https://youtu.be/ckcDDAdR-Ro
128. A ride in Chicken World has limits to who can ride. Customers must be at least 135 CM tall and must
not be pregnant.
Create an algorithm that:
• Asks the user to input the height in CM.
• Ask if they are male or female. If female, ask if they are pregnant.
• Repeat the process until 4 people are on the ride.
• Display if they can ride or not.
• Display “The ride is now full, enjoy your ride!” after 4 people have been allowed to ride.
Paste your code below:

https://youtu.be/_w-w4nOAoxk
129) A cat café wants to know how the number of males and females entering his store in a single day and wants to
create an algorithm for it. Create an algorithm that:
• Ask the user to either type male, female or quit.
• If the user picks male, add 1 to a variable called male.
• If the user picks female, add 1 to a variable called female.
• If the user picks “quit” then display the total number of males and females picked.
• Otherwise display "wrong option".
• The program repeats until the user enters quit.
Paste your code below:

https://youtu.be/qAZT9jiV55E
130. A supermarket is adding new measures to stop stock piling. Create an algorithm that allows the
customer to buy a maximum of 1 hand sanitiser and 1 toilet roll and a maximum of 2 of the same items.
The customers can not spend over £100.
Create an algorithm that:
• Asks the customer how many hand sanitisers do they want.
• Asks the customer how many toilet rolls do they want to buy.
• Asks the customer if they have more than 2 of the same item.
• Asks the customer for the overall cost of their items.
• Cast each variable to an appropriate data type.
• Outputs an error message and repeatedly asks the questions above until valid inputs have been
entered.
• Output a suitable message when valid inputs have been entered.
Paste your code below:

https://youtu.be/CACnc5IK2Jk
Homework: Revise for end of unit assessment.
Lesson 14
End of unit test
NOW:
Complete end of unit assessment.

MS Form assessment - test link:


https://forms.office.com/Pages/ResponsePage.as
px?id=NRrmRxWbmk-P3e1XFrwgvUKZUnHi_FpM
tvQIsO11-sxURDQ0RkJHNTFQWDA2UTEwRFpK
VVBDNEw3TC4u

THEN:
Complete any unfinished tasks from previous lessons.
Homework:
Watch the video: https://youtu.be/Og_mi14cxMA
Programming technique workbook.
Part 3

Name:

Class:
Revisit phase – Starter
Log on kahoot:

https://create.kahoot.it/share/y10-python-fo
r-loops-and-outputs/b386c8e4-ace7-434f-88
37-2ef28efccebe
Lesson 15 Sunday, June 30, 2024

Random numbers
Lesson objectives…
Use random number generator to solve
problems.
Use different programming constructs
with random number generator.
Solve real world problems using random
number generator.
Theory:
Watch the video: https://youtu.be/Og_mi14cxMA
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:
import random
To display a random number between 1 and 100,
use this code :

import random
num = random.randint(1,100)
print(num)
RANDOM NUMBERS
The following code will store 2 random numbers
then multiplies them by each other.
Tasks
Complete task 131 – 137
Then complete any unfinished tasks from previous
lessons.
131.
• Store a random number from 1 to 100 in variable called “num1” using random.randint.
• Display the random number THEN
• Ask the user for a number between 1 to 10 and store it in another variable called num2.
• Calculate the average of the 2 numbers.
• Display the average.
Paste your code below:

https://youtu.be/zdL1GPTnXGk
132.
• Generate a random number between 1 and 1000.
• Ask the user if they want to see the random number.
• If the answer is yes, display the random number.
• If the answer is “no”, display “ game over” and end the game.
• Display “Wrong option” for any other choices.
• The game repeats until the user enters “no”.
Paste your code below:

https://youtu.be/XASavRVaOCk
133.
• Generate a random number between 1-5.
• Ask the user to guess the number.
• if the guess is correct, display “correct” and add a point to a score variable.
• If the guess is incorrect, display “incorrect” and take a point a way.
• Then ask the user if they want to play again.
• The game will repeat until the user enters “no”.
Paste your code below:

https://youtu.be/3OrfonPZ1iM
134. Create a program that :
• Generates 10 random numbers. All of the random numbers must be between 1 and 100.
• Display each random number.
• Then display whether the number is even or odd.
Paste your code below:

https://youtu.be/ph36Fy84ZcM
135.
• Store 2 random numbers (between 1 and 10) in 2 different variables.
• Ask the user to work out the answer when these two numbers are multiplied by each
other.
• Display whether the answer as right or wrong.
• The above process must repeat 5 times.
Paste your code below:

https://youtu.be/Y5W6uptbShw
136. Create a program that:
• Generates and displays 6 random numbers between 1 and 1000 using a For loop.
• Then displays the highest number and the lowest number generated in a full sentence.
Paste your code below:

https://youtu.be/5QVpGXAqTPE
137. Create a guessing game.
• Store a random number between 1 and 100 in a variable using random.randint(1,100).
• Ask the user to guess it.
• Display a “too high”, “too low” or “Correct” depending on the guess.
• Repeat the question until they guess correctly.
Paste your code below:

https://youtu.be/HMxcj1GIsD0
Homework: Watch the video:
https://youtu.be/ZfQ5XM7i7gk
Revisit phase – Starter
What are the 3 different programming
constructs? Give an example of each.

• Sequence
• Iteration
• Selection
Revisit phase – Starter

Which programming construct was used in


the above program?
• Sequence
Revisit phase – Starter

Which programming construct was used in


the above program?
• Iteration (Count controlled iteration)
Revisit phase – Starter

Which programming construct was used in


the above program?
• Sequence (line 1) & Selection
Lesson 16 Sunday, June 30, 2024

Arrays
Lesson objectives…
Describe the purpose of an array.
Create a program using arrays.
Explain the difference between arrays and
records.
Theory:
https://youtu.be/ZfQ5XM7i7gk
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.
Array name

names
0 1 2 3 Index number
(Starts from 0)
Tom Sam Ron Chi

Contents of the array (Data type string)


Arrays
Advantages of using array?

• Allows multiple items of data to be


stored under 1 identifier.

• Reduce the need for multiple


variables.
Records
Records: 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 record, however it can’t


be stored in an array as it contains multiple data
types
Arrays & Variables
Which one is easier to read and more efficient?

Using array: Using variables:


numbers = [5,4,8,3,6,1,4] number1 = 5
number2 = 4
number3 = 8
number4 = 3
number5 = 6
number6 = 1
number7 = 4
Arrays
Array index starts from 0. Quotation marks are needed around
strings but not around integers.

names = ["Rom","Ash","Sam","Ali","Mark"]

What do you think the following code will display?


print(names[0])
• Rom

print(names[2])
• Sam
Tasks
Complete task 138 – 141
Then complete any unfinished tasks from previous
lessons.
138)
• Create an array called films and store your top 3 films in it.
• Display your array.
• Then display the first film in the array on a separate line.
• Then display the last film in the array on a separate line.
Paste your code below:

https://youtu.be/fSXovg_c5JI
139) Create an array called numbers and store 5 numbers in the array. Display the 2nd
number in the array.
Paste your code below:

https://youtu.be/L4ASjfTz-mA
140)
• Create an array called names and store 3 names inside it.
• Ask the user which index number do they want to see. If the user picks a number that is
outside the index range (above 2) then repeat the question again until a valid number is
entered.
• Display the item on that index number.
Paste your code below:

https://youtu.be/Zq9owChfHeM
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
141. Create an array called singers that contains the following singers:
"Rihanna","Katy","Adele","Ed"
Use 3 print commands to:
• Display the first singer in the array
• Then display the first 3 singers in the array.
• Then display the last singer in the array
Paste your code below:

https://youtu.be/-x698NfpgS8
Arrays
What will the following code display?

• 3
Arrays
You can use a FOR LOOP to loop through an array.
The following code will display all of the contents in the
array on separate lines:
Lets see how it works in a trace table:

x Output
films = ["Saw", "Sharks", "Lion king"]
for x in range(0,len(films)):
print(films[x])
0 Saw
1 Sharks
2 Lion King
The code below asks the user to enter a name, if the
name is inside the array, it will display Found.
Tasks
Complete task 142 – 144
Then complete any unfinished tasks from previous
lessons.
142.
• Create an array called games and store the following games inside it:
"OW","CSGO","FIFA","FORTNITE"
• Use a FOR LOOP to loop through the array and display all 4 games on separate lines.
Paste your code below:

https://youtu.be/d60jdqOLNDI
143.
• Create the following array: numbers = [2,3,4,5,6,7,90]
• Ask the user to guess a number.
• If the number is inside the array, display “found”.
Paste your code below:

https://youtu.be/emDvgB3eMfA
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.
144. 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: https://youtu.be/qFVALKQi5rY
students = ["Tom","","Andy","","Lisa","","","","","Abodi"]
Create an algorithm that:
• Checks each index in the array for a name, if it has a name, move on to the next index, if it doesn’t,
then ask the user for a name and add it to that index.
• Once all 10 names have been added to the array, display the array.
Paste your code below:
Homework: Watch the video:
https://youtu.be/Ghocrd6KDw0
Starter – Revisit phase
What will the following code display?

Answer:
s missing
• Error
What type of error is it?
• Syntax error
Lesson 17 Sunday, June 30, 2024

Arrays & trace table


Lesson objectives…
Complete trace tables.
Solve real life problems using arrays.
Use iteration to loop through an array.
Theory:
https://youtu.be/Ghocrd6KDw0
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
Trace table – Class demonstration:

x letter Output

0 x
0 z zy
1 y yy
2 x
2 z zy
Tasks
Complete task 145 – 152
Then complete any unfinished tasks from previous
lessons.
145. Complete the trace table
new x num

https://youtu.be/Gf05FF6jwoE
146. Complete the trace table – Assume the user enters
the following as inputs: Metallica, Idles
band name Output

https://youtu.be/JQA1Sdj0tP0
147. Complete the trace table
highest lowest num item

https://youtu.be/VjwJk1by91c
148. Complete the trace table – Assume the user enters
5,2,8,1,4 as inputs total num number item

https://youtu.be/JQi4Iqb5rao
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:

https://youtu.be/BVsHqzKcUsA
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:

https://youtu.be/2QVemPiCyTk
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:

https://youtu.be/-BE0fvVZWVo
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:

https://youtu.be/U3QKr5-z4q0
Homework: Watch the video:
https://youtu.be/in2-sWY2eHg
Lesson 18 Sunday, June 30, 2024

Arrays & lists


Lesson objectives…
Understand the difference between arrays
& lists.
Solve real life problems using lists.
Use iteration to loop through a list.
Theory:
https://youtu.be/in2-sWY2eHg
Arrays
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.
Arrays & Lists
• A list is also a data structure that stores multiple
values, under 1 identifier.

• However the data can be of DIFFERENT data types.

• A list can grow and shrink.

• The way you create a list in python is exactly the same


as an Array using [ ]

However if it grows or shrinks while the program is


running, it means it’s a list.
Lists
Append means adding something to the end.
#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)
Lists
Empty list

Adds the name to the


end of the list
Tasks
Complete task 153 – 161
Then complete any unfinished tasks from previous
lessons.
153. 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:

https://youtu.be/GC2uLJKa80M
154.
• Create an empty list (array)
• Ask the user to enter 4 numbers
• Store them in the list (array).
• Display the list (array).
Paste your code below:

https://youtu.be/gT3oYosS2Sw
155.
• Create an empty list (array)
• Ask the user to add 6 numbers to the list (array) using a for loop.
• Display the first number in the list (array)
• On a separate line display the 4th number in the list (array)
• Calculate & display the AVERAGE of the numbers inside the list.
Paste your code below:

https://youtu.be/KuSShb1k4NA
156.
• Liverpool = [55,65,75,65,78,45,76,66,98,102]
• ManCity = [52,61,85,75,68,65,96,86,99,85]
• The above 2 arrays shows the points earned by 2 teams at the end of each season over 10 years.
Write an algorithm that:
• Adds together all the number in each array (Liverpool and ManCity) and store them in separate variables.
• Displays the total points earned by each team over 10 years.
• Then displays which team has the highest number of points over 10 years or whether they have the same
number of points.
Paste your code below:

https://youtu.be/wGdoOlX4058
157. 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:
• Ask the user how many students are in his class.
• Asks for the name of each student.
• Ask if the student is male or female.
• Stores the names of the students in a list, male students in a list called male and female students in a
list called female.
• Displays both lists at the end of the program.
Paste your code below:

https://youtu.be/mhVBRoZrtUg
158)
• Ask the user to enter 6 numbers and store them in a list (array).
• Ask the user if they want to see the total (sum) or the average of the 6 numbers.
• Loop through the list to calculate the sum of the numbers.
• Display the answer depending on the user’s choice.
Paste your code below:

https://youtu.be/0nNcCSP0EcU
Sorting

array = [1,7,2,5,6]
array.sort() Sorts the list in number/alphabetical order.

print(array)
array.reverse()
print(array)
Sorts the list in reverse order.
159.
• Create an empty list (array).
• Ask the user to enter 3 games and store it in an list (array).
• Sort the list (array) in alphabetical order.
• Display the list (array)
• Then ask the user which index number in the list (array) do they want to see.
• Display the value of that index number.
Paste your code below:

https://youtu.be/fZ-XE500jbk
160.
• Create an empty list.
• Ask the user to enter 5 numbers and store them in the list.
• Sort the list in order then display the list.
• Then Reverse the order of the list then display the new list in reverse order.
Paste your code below:

https://youtu.be/TfS7dnpAsYs
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.
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.
161.
• Create an empty list (array).
• Ask the user to input the name of 4 films and store them in a list (array).
• Display the first 2 films in the list.
• Remove the last film in the list hint: use
• Display the list
Paste your code below:

https://youtu.be/AaqqS1LpQWA
Homework watch the video:
https://youtu.be/m4d7F_J7xjQ
Revisit phase - Starter
Define the following terms:

Array:
• A data structure that stores multiple data
under one identifier. All of the data must be of
same data type.

Record:
• A data structure that stores multiple data
under one identifier. Data can be of MULTIPLE
data types.
Lesson 19 Sunday, June 30, 2024

2 Dimensional Array
Lesson objectives…
Distinguish between a 1D and a 2D array.
Create a 2D array in python.
Complete a trace table of a 2D array.
Theory:
https://youtu.be/m4d7F_J7xjQ
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]


2D Array

array = [ [3,2,1], [2,3,4], [7,5,2] ]

print(array[0][1])
Row 0 column 1

What will this code display?


• 2
2D Array

array = [ [3,2,1], [5,3,4], [7,8,2] ]

print(array[1][2])

What will this code display?


• 4
2 Dimensional Array
letters = [ [ "A","B","C"],["G","S","T"] ]
What will the following code display?
print(letters[1][1])
• S
print(letters[0][2])
• C
Column 1 Column 2 Column 3

Row 1 A B C

Row 2 G S T
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
Tasks
Complete task 162 – 169
Then complete any unfinished tasks from previous
lessons.
162) 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

https://youtu.be/KOLFLMPizsc
163.
• Modify number 5 on row 1 column 2 and change it to number 10 using the following
code: array[0][1] = 10
• Then display the new list on the screen.
Paste your code below:

https://youtu.be/PuD3GEz2iTU
164) Complete the trace table.
row grades[row][0] grades[row][1] Output

https://youtu.be/qsjmhxfq5-o
165) Complete the trace table. Assume the user enters the following inputs:
15,40,20,7,55,21
x y num numbers[x][y]

https://youtu.be/xCP2-_Ihw2Y
166) Complete the trace table. Assume the user enters the following inputs: 25,90,50
row Output score grades[row][1]

https://youtu.be/qqGddNS1_k4
167. The table on the right shows student’s names and grades Tom
and is represented the following 2d array:
grades = [["Tom",""],["Mark",“B"],["Kim",""],["Sam",“C"],["Andy",""]] Mark B
Use the above array to create an algorithm that:
• Check if a grade has been added for each student. Kim
• If a grade has been added, display the name and the grade of that student.
• If the grade hasn’t been added, display the student name Sam C
and ask the user to enter a grade for that student. Andy
• Add that grade to the 2d array then display the name of the student with their grade.
Paste your code below:

https://youtu.be/nv-gMU9afuM
168. 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:
• Asks the user to enter the name of the 3 singers and the amount of votes each singer then adds them
to the 2d array above.
• Display the 2d array.
• Then display the name of singers only.
Paste your code below:

https://youtu.be/4SDNMk_gFV4
169. 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
• Loops through the 2D array and checks whether a player is on a red card.
• If a player is on a red card, display the player name followed by the player number
followed by “is on a red card”.
Paste your code below:

https://youtu.be/J1lJRKYnN7M
Revisit Phase / starter:
Log on kahoot.
https://play.kahoot.it/#/k/2cb54137-20c9-45
05-b343-ec9c39b8ea95
Lesson 20 Sunday, June 30, 2024

Revision
Lesson objectives…
Consolidate understanding of iteration
and selection.
Consolidate understanding of 1D and 2D
arrays.
Consolidate understanding of random
number generator.
Tasks
NOW:

Complete any unfinished tasks from previous lessons.

THEN:

Complete task 170 – 180


Then complete any unfinished tasks from previous
lessons.
170. 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]] col1 col2 col3 col4
Create an algorithm that:
• Calculates and outputs the total (sum) of the numbers in the table. Row1 1 2 4 5
• Calculates and outputs how many individual
Row2 3 4 6 7
numbers are present in the table.
• Calculates and outputs the average of the numbers in the table. Row3 2 7 8 9
Paste your code below: row4 8 7 1 2

https://youtu.be/SA-AeqRQ3hI
171)
• Ask the user to enter the name of 3 singers, stores them in an array.
• Sort the array in alphabetical order
• then display each singer in the array on a separate line with their position.
Paste your code below:

https://youtu.be/93LNrg_MJU4
172.
• Ask the user to enter a game
• Display the numbers from 0 to the number of characters in the game entered. (On
separate lines)
Paste your code below:

https://youtu.be/eeUBxcLt2pg
173.
• Generate 5 random numbers between 1 and 100.
• Displays each random number.
• Store the numbers in an array (list).
• Sort the array (list) in order.
• Display the array.
Paste your code below:

https://youtu.be/P3_IYIDQhZM
174.
• Ask the user how many numbers they want to generate.
• Ask the user for the lowest and highest number they want to generate.
• Display each number generated.
• Store the numbers in a array (list).
• Display the list.
Paste your code below:

https://youtu.be/7tvvILE9Gbo
175. Create a program that:
• Asks the user to enter a binary number.
• Counts how many 1s and how many 0s are in the following binary number.
• The program must display the number of 1s and 0s in a full sentence.
• Hint: Use a for loop to loop through the string.
Paste your code below:

https://youtu.be/VRI2X237zTU
176. Use the following array: numbers = [1,5,2,3,7,3,54,2,1,8,6,3,4]
Create a program that:
• Loops through the array to calculate how many odd numbers and how many even
numbers are in the array.
• Display the answer in a full sentence.
Paste your code below:

https://youtu.be/ik3Z1GojenI
177. An isosceles triangle is a triangle with (at least) two equal sides.
• Create 3 variables to allow to the user to enter the 3 lengths.
• Works out if the triangle is isosceles or not.
• If it’s not then display “The triangle is not isosceles, enter new lengths”
• Repeat the process until the user enters lengths of an isosceles triangle
• Then display “The triangle is isosceles.”
Paste your code below:

https://youtu.be/G7hB4omP8aQ
178. Create an algorithm that:
• Asks the user how many items need to be scanned.
• Asks if the item has been scanned correctly.
• If yes, display the item x has been scanned. Where x represent the item number. If no,
display item x has not been scanned correctly. And ask the question again.
• The process repeats until all items has been scanned correctly.
Paste your code below:

https://youtu.be/YLwQoww6KOE
179.
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:
• Loops through both arrays to count and display the number of matches where Aston villa scored at
least 3 goals and the number of matches where Aston villa conceded at least 3 goals.
• Display your answer in a full sentence.
Paste your code below:

https://youtu.be/ntA-IJJFggA
180.
• Create an empty list (array).
• Repeatedly ask the user for a number until the user enters 0 or a negative number.
• Append each number to the list (array).
• Calculate and display the amount of numbers the user entered.
• Calculates and display the total and average of the numbers.
Paste your code below:

https://youtu.be/403jQjxNNPo
Homework watch the video: https://youtu.be/PVLqrEVOlQM
181. Starter: Revisit phase – Complete the trace table Assume the user enters the
following values: 5,106,200,100

start num output

https://youtu.be/SSSC7FGixus
Lesson 21 Sunday, June 30, 2024

File Handling
Lesson objectives…
Explain the purpose of text files.
Write to a text file in python.
Use a For loop to write multiple data to a
text file.
Theory: https://youtu.be/PVLqrEVOlQM
Text files
Text files can be used to save data in a text file
such as score, username and passwords.

Advantages of storing data on a text file:


• Can be accessed again even after you close the
program.
• Data from the text file can also be used for
other programs.
Writing to a file

Tells the program


Variable / Opens/create a file File name we want to “write”
file handler

file = open("test1.txt", "w")


Write
inside the file.write("Welcome\n") Move to a
new line.
file.
file.write("My Friend") Values/text
file.close() that will be
inside the
Closes file.
the file
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.
Example:

Cast num from an integer


to a string.

When writing a number to a text


file. You must CAST it to a string,
otherwise you will get an error.
Writing to a file

Variable / File name


Opens/create a file
file handler write mode

file = open("names.txt","w") Repeat the


Write for x in range (4): process 4
inside the times.
file. name = input("Enter name")
file.write(name+"\n")
file.close() Asks user
Closes for a name.
the file
Move to a
new line.
Tasks
Complete task 182-190

Then complete any unfinished tasks from previous


lessons.
182. In python
• Create a text file called "hobby.txt "
• Write " football " to the text file.
• On another line write "Basketball "
• Then close the file.
Paste your code below:

https://youtu.be/mRhOm-Owp_o
183. In python
• Create a text file called "singers.txt "
• Ask the user for their favourite singer then write it to the text file.
• Close the text file.
Paste your code below:

https://youtu.be/nPOyhnpFb3k
184. In python
• Create a text file called "celebrities.txt "
• Ask the user for their top 5 favourite celebrities (use a for loop)
• then write each celebrity name in the text file on a separate line.
• Close the text file.
Paste your code below:

https://youtu.be/fEH28Qyiqmc
185. In python
• Create a text file called "numbers.txt "
• Ask the user to enter 6 numbers (use a for loop).
• then write each number in the text file on a separate line.
• Close the text file.
Paste your code below:

https://youtu.be/9MfqFvXYjHg
186. In python
• Create a text file called "numbers.txt "
• Asks the user for a number.
• If the number is greater than 100 then write it in the text file.
• Otherwise display "Number is not big enough" on the screen.
• Repeat the process 4 times.
• Close the text file.
Paste your code below:

https://youtu.be/vTdlqYRCKOk
187. In python
• Create a text file called "data.txt"
• Ask the user if they want to write to the text file.
• If the answer is "yes", ask the user for a sentence to write.
• Repeat the process until the user enters any input other than "yes" when asked if they
want to write to the text file.
• Close the text file.
Paste your code below:

https://youtu.be/fLP07bUeehE
188. In python
• Create a text file called "newfile.txt"
• Ask the user for a username and password.
• Repeat the questions until they enter the correct username and password. Assume the
correct username is "Luther" and the correct password is "bob123" .
• When the correct username & password have been entered, write them to the text file.
• Close the text file.
Paste your code below:

https://youtu.be/n1fyEZyx99Q
189. In python
• Create an empty list (array)
• Asks the user to enter 4 names and append them to the list (array).
• Creates a text file called "names.txt"
• Loop through the array and write each name in the array to the text file.
• Close the text file.
Paste your code below:

https://youtu.be/Aa-STeUMTGY
190. A teacher wants to write the names and grades of his students in a text file:
• Create a text file called "grades.txt"
• Asks the user how many students are in his class.
• Ask for the name and grade of each student and write it to a text file on separate lines.
• Close the text file.
Paste your code below:

https://youtu.be/b1-71ctWnqY
Homework watch the video:
https://youtu.be/WJXjdaEE2bg
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
write
Should be 1
parenthesis

Should be
name without
Missing () an s.
Lesson 22 Sunday, June 30, 2024

File Handling
Lesson objectives…
Demonstrate appending to a text file.
Demonstrate reading from a text file.
Demonstrate Looping through a text file.
Theory:
https://youtu.be/WJXjdaEE2bg
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.
Appending to a file

Tells the program


Variable / Opens/create a file File name we want to
file handler “append”

file = open("test1.txt", "a")


Write
inside the file.write("Welcome\n") Move to a
new line.
file.
file.write("My Friend")
file.close()
Closes
the file
Reading from a text file

Variable/ Opens the file File name Read mode


file
handler
Read the
file= open("names.txt", "r") content of the
readit = file.read() file and stores
it in a variable
file.close() called readit.
Closes
the file print(readit)

Displays the content of readit


Reading from a text file

Variable/ Opens the file File name Read mode


file
handler
file= open("names.txt", "r")
Loops through
name = input("Enter name") each line in the
for line in file: text file.

if line == name:
if the name is print("Found")
in the file,
then it will
file.close()
display Found.

Closes
the file
Tasks
Complete task 191-192

Then complete any unfinished tasks from previous


lessons.
191.
• Create a text file called "singers" (write mode).
• Ask the user to enter the name of a singer and store it in the text file.
• Close the text file.
• Now open the text file in read mode.
• Read the content of the file then display it on the screen.
Paste your code below:

https://youtu.be/wTIZODk7MS4
192.
• Ask the user to specify a filename.
• Create a text file with the name that the user entered.
• Ask the user to enter a hobby.
• Write the hobby in the text file then close the text file.
Paste your code below:

https://youtu.be/clQPX7rE0bg
193. For this question, copy and paste your code from question 192 and put it at the top. You will be
adding to it now.
• Open the text file from question 192 in append mode.
• Ask the user to enter a name.
• Write the name in the text file.
• Close the text file.
• Open the text file in read mode and read the contents of the text file.
• Close the text file then display the contents of the file on the screen.
Paste your code below:

https://youtu.be/ridnqDyJhQU
194.
• Ask a user if they want to read or write.
• If the answer is write, then create a text file called "colour.txt"
• Ask the user for their favourite colour then write it to the text file.
• If the answer is read, ask the user which file do they want to read.
• Read the content of that file (make sure that the file exists to be able to read from it) then display
the content of the file on the screen.
• Display "Wrong answer" for any other choices.
• Close the text file.
Paste your code below:

https://youtu.be/-PTRdzrWnak
195.
• Create a file called numbers.txt (write mode)
• Ask the user how many numbers they want to enter.
• Depending on the user’s answer, ask the user for that many numbers and write them inside the
text file.
• Close the file.
• Open the file in read mode, read the numbers inside the text file and display them on the screen.
Paste your code below:

https://youtu.be/fqphPoG4CME
196.
• Create a file called data.txt (write mode)
• Ask the user if they want to enter their firstname or surname in the file.
• Depending on the user’s answer, ask them for their firstname or surname and write it inside the
file.
• Close the file.
• Repeatedly ask the question until the user enters “firstname” or “surname” when asked whether
they want to enter their first name or surname.
Paste your code below:

https://youtu.be/dpYl5NzLrc8
197.
• Create a file called "newdata.txt" (write mode) .
• Ask the user for 5 usernames and write them in the text file. Then close the file.
• Open the file in read mode.
• Ask the user to enter a usernames that is inside the file.
• Loop through each line in the file, if the usernames is inside the file, display “Found”.
Paste your code below:

https://youtu.be/Zl17Cdljib0
198.
• Create a file called "games.txt " (write mode) .
• Ask the user to enter the name of 3 games and write them inside the file.
• Close the file.
• Open the file in read mode and read the contents of the file.
• Ask the user if they want to see the contents of the file in uppercase or lowercase.
• Display the content of the file according to the user’s answer.
Paste your code below:

https://youtu.be/1mxSDaeXm68
Extension
Complete task 199 & 200 if you finished all of the tasks.
199. Part 1 - A company hired Jim to watch 5 films then rate each film out of 10.
Create an algorithm that:
• Asks for film name.
• Asks for the film rating,
• if the rating is above 7, store the film name in an a list (array) called
• films and the rating in a list (array) called rating.
• The process repeats until all 5 films have been rated.
• Loop through the arrays to display the film names and the ratings.
Paste your code below:

https://youtu.be/0b28TUpplTM
200. 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.
• Your program must ask the same question for each film name that received a score of 8 or above.
• If the user answers yes, write the film name and its rating to a text file called “filmratings.txt”.
Paste your code below:

https://youtu.be/WbKKKNleowE
Homework watch the video: https://youtu.be/ualSFRIclz8
Revisit phase / Starter

What are the 3 programming


constructs:
Sequence
Selection
Iteration
Lesson 23 Sunday, June 30, 2024

Subroutine: Procedure
Lesson objectives…
Demonstrate programming a simple
procedure.
Describe the purpose of subroutines,
parameters and arguments.
Explain the advantages of using
subroutines.
Theory: https://youtu.be/ualSFRIclz8
Procedure

def is a procedure name


command
Ask the
which allows
def names(): user to
you to define a
choice=input("Enter a name") input a
procedure or a print(choice) name.
function. names()
names()
Calls the Displays the
procedure. This user’s
will run line 2 answer.
and 3.
Procedure

Procedure name.
Parameter.

def is a command
which allows you def welcome(name):
to define a print("Welcome to school" , name )
procedure or a Displays
welcome("Alex") welcome to
function. welcome("Tom") school + the
value of the
argument.
Calls the
procedure. This
will run line 2. Argument.
Procedure
What will the program display?

Parameters

def calculate(num,num2):
print(num * num2)
calculate(2,4) Local Variable

calculate(5,6)
The values 2 and 4 are passed into num
and num2.
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
Procedures
• Subroutine (sub-program): Self-contained
section of code.

• Parameters: Special variables used in


subprograms to pass values into a sub
program.

• Arguments: Actual values that the


parameters take when the sub program is
called.
Advantages of subroutine
• The program becomes easier to read.
• The program becomes easier to test as you
can test individual subroutines one at a
time.
• Different programmers can work on
different subroutines which increases the
speed of development.
• Subroutines can be reused so the code
does not need to be rewritten.
Tasks
Complete task 201-211

Then complete any unfinished tasks from previous


lessons.
201.
• Create a procedure called “name” that will ask the user for their name.
• Display the name.
Paste your code below:

https://youtu.be/kOc9mtsDX8s
202.
• Create a procedure called “numbers” that will ask the user for 2 numbers.
• It will then divide the first number by the second.
• Display the answer.
Paste your code below:

https://youtu.be/2WFtJ1WBtrA
203.
• Create a procedure called “game” that takes a number as a parameter.
• It will display “over 9000s” if the argument is over 9000, otherwise it will display “You
need to power up”.
• Use number 8 as the argument.
Paste your code below:

https://youtu.be/bXtV93phb6E
204.
• Create a procedure called “oddoreven” that will take a number as a parameter.
• It will then display “Odd” if the number is odd or “even” if the number is even.
• Use number 4 as the argument.
Paste your code below:

https://youtu.be/A15dd_cVa0E
205. Procedure trace table - Assume the user enters the
following values: 88,155,15,102,100
Repeat Repeat == True num output num==100

https://youtu.be/4gS_QudghtU
206. Procedure trace table - Assume the user enters the
following values: fortnite, minecraft, overwatch
x game age output

https://youtu.be/Iv088BB_ABs
207.
• Create a procedure called “count” that takes a number as a parameter.
• Checks if that number is above 100, and displays an error message if the number
(parameter) is above 100.
• Otherwise it will display all the numbers from that number (parameter) up to and
including the number 100.
• In the main program, ask the user for a number and use it as the argument.
Paste your code below:

https://youtu.be/QslvpM2TWL0
208.
• Create a procedure called “multiply” that takes a number as a parameter.
• The procedure will then display the times table of that number from 1 to 100.
• Use 4 as the argument.
Paste your code below:

https://youtu.be/LvBmT-7mxUM
209. https://youtu.be/HERiKJxf-40
• Create a procedure called “name” that ask the user for a name then displays it on the screen.
• Create another procedure called age that asks the user for age then displays it on the screen.
• In the main program, ask the user if they want to run the name procedure or age procedure.
• If they pick name, run the “name” procedure, if they pick “age”, run the age procedure.
• Display “Wrong option” for any other choices.
Paste your code below:
210.
• Create a procedure called “countup” that takes a number as a parameter.
• The procedure will display all numbers from 1 up to that number.
• Use 6 as the argument.
• Example: if the argument was 6, the procedure will return: 1,2,3,4,5 and 6.
Paste your code below:

https://youtu.be/jpvp-qXErOQ
211.
• Create a procedure called “writing” that takes the user’s name & age as a parameter.
• Writes the user’s name & age in a full sentence to a text file called “information.txt”.
• Use Aryan and 24 as the arguments.
Paste your code below:

https://youtu.be/cGQN-QbgNqA
Homework
Watch the video: https://youtu.be/kZA_2c7txl0
Revisit/starter: Advantages of subroutine?
• The program becomes easier to read.
• The program becomes easier to test as you
can test individual subroutines one at a
time.
• Different programmers can work on
different subroutines which increases the
speed of development.
• Subroutines can be reused so the code
does not need to be rewritten.
Lesson 24 Sunday, June 30, 2024

Subroutine: Function
Lesson objectives…
Demonstrate programming a simple
function.
Explain the difference between global and
local variables.
Explain the the difference between a
procedure and a function
Theory
Watch the video: https://youtu.be/kZA_2c7txl0
Functions
A function is a subroutine that
returns a value.
Function

Function name.

Causes the
function to
def name():
exit and hand
choice = input("Enter name") back the
return choice value of
print(name() ) Local Variable choice to its
caller

Calls and displays the returned value of the function


Function

Parameters

causes the
def multiply(num1,num2): function to
exit and hand
answer = num1 * num2 back the
return answer value of
print(multiply ( 2 , 8 ) ) Local Variable answer to its
caller

Arguments, the values 2 and 8 are passed into num1 and num2.
Functions & Procedures
Similarities and differences between function and
a procedure

• Both functions and procedures are used to


structure code and create reusable
component.
• Function returns a value, a procedure does
not.
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.
Variable Scope

Local

Global
Variable Scope
Identify the local and global variables in the example below:

Local

Global
Tasks
Complete task 212-220

Then complete any unfinished tasks from previous


lessons.
212.
• Create a function called “multiply” that takes a two numbers as a parameter.
• The function will return the value of the first number multiplied by the second number.
• Use 2 and 5 as the arguments.
Paste your code below:

https://youtu.be/cj8Mng6BrFs
213.
• Create a function called average that takes 3 numbers as parameters.
• The function will return the average of the 3 numbers.
• Use 4,7 and 10 as arguments.
Paste your code below:

https://youtu.be/Tt2O0I37YGw
214.
• Create a function called “areacircle” that takes radius as a parameter.
• Your function must calculate and return the area of a circle.
• Area of circle formula: Pi * RADIUS²
• Pi = 3.14
• Use 5 as the argument.
Paste your code below:

https://youtu.be/xSoyUiZkzAc
215.
• Create a function called “largest” that takes 3 numbers as parameters
• The function then returns the largest of the three numbers.
• Use 6,4 and 5 as arguments.
Paste your code below:

https://youtu.be/JBuriUlNkn8
216.
• Create a function called “convert” that takes a temperature in Celsius as a parameter.
• Converts the temperature from Celsius to Fahrenheit.
• Return the temperature in Fahrenheit.
• Use 10 as the argument.
• Formula: Y°F =(X°C × 9/5) + 32
• X represents the value in Celsius and Y represent the value in Fahrenheit.
• Use 10 as the argument.
Paste your code below:

https://youtu.be/LXBvy7SKoLE
217.
• Create a function called “filesize” that calculates and returns the file size of an image.
• The function takes width, length and bit depth and DPI as parameters.
• Formula to calculate file size in bits - Formula: Height x Width x DPI x DPI x Bit Depth
• In the main program, ask the user for width, length, bit depth and DPI and use them as
arguments.
Paste your code below:

https://youtu.be/044CdEAGP5o
218. 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:
• Take player level as a parameter.
• If a player level is 50 or above, then it sets their level to 60.
• If a player level is below 50, then it adds 10 levels to the original player level.
• Return the new “playerlevel”.
• Test your program by trying different numbers in the argument.
Paste your code below:

https://youtu.be/Kmg8qYPCSn0
219.
Create a function called “game” that takes a number as a parameter.
The function must:
• Return over 9000 if the number is above 9000.
• Return it’s 9000 if the number is 9000.
• Return less than 9000 if the number is less than 9000.
• Test your program by trying different numbers in the argument.
Paste your code on the below:

https://youtu.be/VE0va9voJXI
220. 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:
• Ask the user for the current time in hours followed by minutes, example: 16.30 which is 4.30pm.
• If the time entered is not between 8 and 20, then display restaurant is currently closed and ask the question
again until the user enters a valid number.
• Calculate and return the total price taking into account the number of adults, number of children and entrance
time.
• In the main program, ask the user for the number of adults and children and use them as arguments.
Paste your code on the below:

https://youtu.be/o4V7570hVoE
Homework
Watch the video: https://youtu.be/GI-aBknEQ-8
Revisit phase
1- Describe the differences between local and global
variables
• Local variables are declared in subroutines.
• Global variables are used throughout the entire
program.

2- Explain why it is better to use local variables?


• Local variables are used when they are not needed
throughout the entire program.
• It uses memory only when needed.
• It is harder to find errors in global as they can be
accessed throughout the entire program.
Lesson 25 Sunday, June 30, 2024

Validation, authentication & verification


Lesson objectives…
Explain the purpose of authentication &
verification.
Explain the purpose and types of input
validation.
Demonstrate coding programs with input
validation.
Theory
Watch the video: https://youtu.be/GI-aBknEQ-8
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.
Verification
Verification: Used to double-check that the data has
been typed in correctly.

Example: Double entry of password.


Validation
Validation: Checking data entered meets certain criteria.

Reasons:
• Reduce errors.
• Stop programs from crashing.

Types of validation:
1. Range check
2. Length check
3. Presence check
4. Type check
Validation – Range check
Range check: Checks if the data is within certain
range.

Example: Number between 1 and 100


Validation – Length check
Length check: Checks the length of the string is
valid (acceptable).

Example: Password with at least 8 characters.


Validation – Presence check
Presence check: Checks that data has actually
been entered.

Example: Checks if the username has been


entered.
Validation – Type check
Type check: Checks that the data entered is of
valid (acceptable) data type.

Example: Integer (whole number) when asked


about age.
Tasks
Complete task 221- 227
Then complete any unfinished tasks from previous
lessons.
221. 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:
• Asks the user to enter number of tickets.
• Validates that the number entered is between 5 and 20. If not repeat the question until a valid
number is entered.
• When a valid number is entered, display “See you soon!”
Paste your code below:

https://youtu.be/B7GtvGTivNQ
222. Length check validation program:
A website require users to have usernames between 8 and 15 characters.
Create a program that:
• Asks the user to enter a username.
• Repeatedly ask the user to enter a username until the user enters a username with the correct
number of characters. (between 8 and 15)
• When a valid number is entered, display “Username accepted”
Paste your code below:

https://youtu.be/z2xeq8Bfo0Y
223. 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:
• Asks the user to enter a name.
• Checks if the user has entered something, if they haven't, then repeat the same question again until
they enter a name.
• When the user enters a name, display “Order received”.
Paste your code below:

https://youtu.be/WpZJARFmnYA
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
224. Type check validation program:
Create a program that:
• Asks the user for the number of goals scored in a match.
• If the user enters a letter or a symbol or a decimal number, display “You must enter a
whole number” and repeat the question.
• When the user enters a whole number, display “Score accepted”.
• Use the code below of a similar example to help you:
Paste your code below:

https://youtu.be/gevM8G_W-g8
225. Verification program:
Create a program that:
• Asks the user to enter a username and store it in a variable called “username1”
• Asks the user to enter the user again and store it in a variable called “username2”
• If both usernames are the same, display “username accepted”. Otherwise repeat the
process above until both usernames are the same.
Paste your code below:

https://youtu.be/SknkeMlsczo
226. Authentication program:
Create a program that:
• Asks the user for a name and age.
• If the name entered is “Sam” and age entered is “15”, display “Verified”. Otherwise
repeatedly the user to enter a name and age until they enter the correct details for both
the name and age.
Paste your code below:

https://youtu.be/JoteRziuJcc
Extension
Complete tasks 228- 230
Then complete any unfinished tasks from previous
lessons.
228. Create a program that:
• Asks the user to enter a pin code that contains the number 1, 2 and 3 only.
• If the code valid (must contain only 1s and 2s and 3s) then display “Pin code accepted”.
• Otherwise ask the user to enter the code again.
• Hint: Use a for loop to loop through the string.
Paste your code below:

https://youtu.be/hIQkBBueVvQ
ord() is a function that returns the ascii code for
the character.
Example: ASCII code for ‘A’ is 65, ‘a’ is 97, etc.
Example: ord("a") is 97

chr() is the opposite of ord()


chr() is a function - it gives the corresponding
character for given ASCII code. For example, to find
the letter of the ASCII code 97 we write:
chr(97) is ‘a’
229. Create a program that displays the ascii code for the letter “C” and then displays the
character for the ascii code 98.
Paste your code below:

https://youtu.be/cCXcNghHzig
230. Ask user for name, convert each letter to character code then add the number (code) to
a total and display the total.
Paste your code below:

https://youtu.be/4GjCELMxFbk
Lesson 26
End of unit test
NOW:
Complete end of unit assessment.

MS Form assessment - test link:


https://forms.office.com/Pages/ResponsePage.as
px?id=NRrmRxWbmk-P3e1XFrwgvUKZUnHi_FpM
tvQIsO11-sxUNFk4NVE1TkhDQVU4RkU4MUIzUT
hWRDY1Ti4u

THEN:
Complete any unfinished tasks from previous lessons.

You might also like