Programming Workbook FSH
Programming Workbook FSH
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.
• Print(“Welcome Tom”)
• print(“Football”
• print(Dancing)
Print function
1- Type the following in python:
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.
Displays the
Variable called user’s answer
“name”
Displays the
Variable called user’s answer
“name”
Variable called
“name”
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
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.
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
Examples:
age = 14 number = -4
score = 75 applenumber = 3
siblings = 5
Real/Float
Real numbers can have a decimal place.
Examples:
Examples:
Symbol = % Grade = C
Gender = M Initial = B
Boolean
Holds either TRUE or FALSE
Examples:
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.
Example:
age = str(age)
Name = “Martha”
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
https://youtu.be/q3MbsyeCA2U
14. Asks the user to enter 3 temperatures, calculate and display the
average and total of the 3 temperatures.
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
Go on
www.kahoot.it
https://play.kahoot.it/#/k/c5703103-9a6e-4b44-8d72-5f7
35c75fe45
Subtraction -
Multiplication
Division
Exponent “^”
Modulo “MOD”
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
If Then
Else
Structure of an if statement
Asks the user to input a
Variable name
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")
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
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
https://play.kahoot.it/#/k/20e5c6d0-9106-4711-88a1-ee
240367208a
Example:
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:
• if name != “hello”
• else::
Logic Error
Logic Error: When your program runs however it
produces an unexpected results.
Examples:
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
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
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 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:
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
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])
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
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
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
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
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
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() )
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
for __ in range(__) :
code to be run here
more code to be run here
this code not in the for loop
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
Start from
Variable
Up to but not included
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
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 () )
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
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
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
https://youtu.be/VyrDzifn_s4
93. Complete the trace table
x y Output
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
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 y in range(100,0,-1) :
print(y)
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
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
Theory:
https://youtu.be/gn6RIuipDBw
Knowledge phase
Colon
Condition
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:
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
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
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
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
1 1 True no 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”
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:
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
• 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
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
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.
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
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.
names
0 1 2 3 Index number
(Starts from 0)
Tom Sam Ron Chi
Name Price
Biscuit 2.5
Crisps 1.5
names = ["Rom","Ash","Sam","Ali","Mark"]
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
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
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.
print(array[0][1])
Row 0 column 1
print(array[1][2])
Row 1 A B C
Row 2 G S T
You can use a nested For Loop to loop through a 2d array
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:
THEN:
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
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.
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 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
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
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
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
17 19 Almost
Procedures
• Subroutine (sub-program): Self-contained
section of code.
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
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
Local
Global
Variable Scope
Identify the local and global variables in the example below:
Local
Global
Tasks
Complete task 212-220
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.
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.
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
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.
THEN:
Complete any unfinished tasks from previous lessons.