Programming technique workbook.�Python 1 section
Name:
Class:
Created by Mr Suffar
Python Introduction
Lesson 1
Thursday, June 15, 2023
Lesson objectives…
Created by Mr Suffar
Video
Lesson 1 - Theory
Created by Mr Suffar
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.
Created by Mr Suffar
Syntax error
Syntax: The order of instructions or commands.
Syntax Error: When you break the rules of the programming language.
Examples:
Created by Mr Suffar
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.
Created by Mr Suffar
Variables & Constants
Variable: A location in a memory where data is stored. It can be changed while the program is running.
Constant: A location in a memory where data is stored. It cannot be changed while the program is running.
Created by Mr Suffar
Variables & Constants
film = "Frozen"
print (film)
film is the identifier of a variable. Used to store the value "Frozen"
Displays the value of the variable.
Value
The above program will display:
Created by Mr Suffar
Variables & Constants
number is the identifier of a variable. Used to store the value "5"
Displays the value of the variable.
Value
Created by Mr Suffar
Concatenation
Concatenation: Joining things “String” together.
In python, we can use the plus sign "+" to concatenate.
What will the above program display?
BobTim
Created by Mr Suffar
Concatenation
Concatenation: Joining things “String” together.
In python, we can use the plus sign "+" to concatenate.
What will the above program display?
Bob Tim
Created by Mr Suffar
Concatenation
film = "Lion King"
print ("My favourite film is " + film)
Concatenation: Joining things “String” together.
In python, we can use the plus sign "+" to concatenate.
What will the above program display?
My favourite film is Lion King.
Created by Mr Suffar
Concatenation
film = "Lion King"
print ("My favourite film is " + "film")
What will this program display?
My favourite film is film
You must NOT put quotation marks around variables.
Created by Mr Suffar
Joining strings with a space
film = "Lion King"
print ("My favourite film is", film)
You can use a comma "," to join 2 strings together WITH A SPACE.
What will the above program display?
My favourite film is Lion King.
Created by Mr Suffar
Concatenation
What will this program display?
Sam Smith
Created by Mr Suffar
Asks for a name, wait for an answer, then displays the name on the screen.
Input function
Variable identifier called “name”
Displays the user’s answer
The question
Allows you to ask a question
Created by Mr Suffar
Input function
Variable identifier called “name”
The question
Allows you to ask a question
Created by Mr Suffar
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:
Created by Mr Suffar
Tasks
Complete the tasks.
Created by Mr Suffar
1. Copy the code below, run it and test it, then explain the purpose of the program/what the program does.
school = input("Which school do you go to?")
teacher = input("Who is your favourite teacher?")
print("I go to",school,"and my favourite teacher is",teacher)
pet = input("Enter favourite pet")
print(pet*10)
Explain the purpose of the code below:
Answer:
Created by Mr Suffar
2. The program below must: Display Welcome user, ask user for name, and sport team, display them in a full sentence. Then ask for surname and use concatenation to display the full name. Then ask which drink they like and display it in a sentence.
Copy the code below, run it and fix the errors.
print"Welcome user"
name = imput("Enter your name"
team = input("Enter a sport team you support)
print("My name is",name"and I support",teams)
surname = input"Enter surname")
print("your full name is "name+ " " + surnme)
choice input(Do you like Tea or Coffee?")
print(choice "is a great option")
Paste the correct version below:
Created by Mr Suffar
3. The program below must: Store the value Rectangle in a variable called shape. It stores 5 inside the variable width and 4 inside the variable length. It then calculates the area of the rectangle by multiplying the width by length. It will then display the area in a full sentence. It will then ask the user for a shape that contains 3 sides and waits for an answer.
Copy the code below, run it and fix the errors.
shape = Rectangle
width = "5"
length = "4"
area width * length
print("The area of the",shape"is"area)
newshape= input("Triangle")
Paste the correct version below:
Created by Mr Suffar
4. The program below must: ask the user for school email and password then displays access granted. Then store the number 5 in a variable called number and dislay 5 is an odd number using the variable number.
Copy the code below, run it and fix the errors.
email = ("Enter your school email address")
password = input("Enter your password
primt("Access granted")
anime "One Punch Man"
rate = input(Rate the following anime: "+anime)
number = "5"
print(number"is an odd number")
Paste the correct version below:
Created by Mr Suffar
5. The program below must ask the user to enter a colour then store "Rose " in a variable called flower. It then print the colour followed by the flower. It then asks user to enter favourite game. It stores the number 5 inside a variable called number then it displays the game name 5 times.
colour = ______("Enter a colour")
flower = "_____"
print(colour,_____)
_____ = input("Enter your favourite game")
number = ___
print(_____*number)
Copy the code above, fill the gaps then paste your code below:
Created by Mr Suffar
6. The program below must ask user for a capital and display it in a full sentence. It then asks for city and displays it in a full sentence. It then stores the number 8 in a variable called squareside. It then calculates the areaofsquare using the value from squareside.
capital= ______("Enter the capital of England")
print("The capital of England is",______)
______ = input("What city do you live in?")
print(_____,"is a great city to live in")
squareside = ____
areaofsquare = squareside * _______
print("The area of a square with side length of 8 is",_______)
Copy the code above, fill the gaps then paste your code below:
Created by Mr Suffar
7. Display the following on 7 separate lines:
Paste your code below:
Created by Mr Suffar
8. Create a variable called food and store your favourite food inside the variable (YOU DO NOT NEED TO USE THE INPUT COMMAND.) Display the value of the variable onto the screen.
Paste your code below:
Created by Mr Suffar
9. 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.
Paste your code below:
Created by Mr Suffar
10. Ask the user for their favourite film. Display the fill name in a full sentence.
Paste your code below:
Created by Mr Suffar
11. Create a program that ask the user for their name. Display the name 1000 times. Use * to multiply.
Example:
Paste your code below:
Created by Mr Suffar
12. 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.
Paste your code below:
hint:
Created by Mr Suffar
13. Ask the user for their name and favourite hobby. Display “your name is”, name, “and your favourite hobby is”, hobby. Example:
Paste your code below:
Created by Mr Suffar
14. Ask the user for a username. Then display “Welcome”, username.
Paste your code below:
Created by Mr Suffar
15. Ask the user to enter a random symbol such as #. Then display that symbol 100 times.
Paste your code below:
Created by Mr Suffar
16. Ask the user for their favourite colour. Then display “Your favourite colour is”, colour.
Paste your code below:
Created by Mr Suffar
Question 17
Answer:
Created by Mr Suffar
Question 18
Answer:
Created by Mr Suffar
Question 19
?
?
?
?
?
Created by Mr Suffar
Homework
Created by Mr Suffar
Log on python, open your programming workbook
THEN
Create a program that asks the user if they like school or not, then display the answer.
Created by Mr Suffar
Data types
Lesson 2
Thursday, June 15, 2023
Lesson objectives…
Created by Mr Suffar
Data types
Created by Mr Suffar
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.
Knowledge phase
Created by Mr Suffar
It is important to use the correct data type to store information because: Using the correct data type will make your programs more memory efficient. It will also make your programs more robust and predictable.
Data types
Created by Mr Suffar
number = -4
score = 75
age = 14
applenumber = 3
siblings = 5
Examples:
A whole number.
Integer
Created by Mr Suffar
Price = 1.84
weight = 7.5
Temperature = 18.5
Number= -4.00
Examples:
Any number that could be a whole number or a fraction.
Real / Float
Created by Mr Suffar
Grade = “C”
Gender = “M”
Symbol = “%”
Initial = “B”
Examples:
A single character.
Character
Created by Mr Suffar
Access= False
Repeat = True
Denied = True
Underweight = False
Examples:
Only one of two possible values - either True or False
Boolean
Created by Mr Suffar
Team = “Aston Villa”
Phone = “07499999222”
Name = “Brad”
Pin = “0415”
Examples:
A group of characters/text
String
Any number that starts/can start with a 0 is a STRING and is
NOT an INTEGER.
String values must have Quotation marks around them.
Created by Mr Suffar
Casting: Change a data type from one to another.
Example of casting in python:
num = 5
converted = str(num)
The above will change the data type of num from INTEGER to STRING and store it in a variable called converted.
num = “5.5”
convert = float(num)
The above will change the data type of num from STRING to FLOAT and store it in a variable called convert.
Casting
Created by Mr Suffar
Casting
Casting: Changing a data type from one to another.
Example: age=int(input(“How old are you?”)) age = str(age) Above line will CAST age from integer to string. |
Created by Mr Suffar
COLD CALL
✓
✓
✓
✓
✓
Created by Mr Suffar
COLD CALL
Created by Mr Suffar
COLD CALL
Created by Mr Suffar
COLD CALL
Changing a data type from one to another.
In the code above concatenation is used to join the balance to a printed message. Balance variable was casted to a string.
Created by Mr Suffar
Easy version:
https://create.kahoot.it/share/data-types/1e607505-b2ee-4749-9dc4-5f203c50436d
Hard version:
https://create.kahoot.it/share/data-type-hard/0be31660-e005-42bb-94ba-7f4d6f9c53c3
Consolidate understanding of data type
Created by Mr Suffar
Python task
Copy the following code in python then click run and put any number in.
Created by Mr Suffar
Python task
Now copy this:
Created by Mr Suffar
Tasks
Created by Mr Suffar
20. Complete the following table:
Data | Data Type |
Age = 15 | |
House address = “25 James road” | |
Name = “Martha” | |
Phone number = “07777797007” | |
Temperature = 15.5 | |
Price = 10.0 | |
Adult = True | |
Gender = “M” | |
Created by Mr Suffar
Question 21
Answer:
Answer:
Answer:
Created by Mr Suffar
22. Copy the code below, run it and test it, then explain the purpose of the program/what the program does.
win = int(input("How many wins does Real Madrid have"))
draw = int(input("How many games ended in a draw for Real Madrid"))
totalpoints = (win*3) + (draw*1)
print("Total points for Real Madrid this season: ",totalpoints)
ticketcost = float(input("How much does it cost to watch Real Madrid?"))
print("Ticket price for this season is:", ticketcost)
Explain the purpose of the code below:
Answer:
Created by Mr Suffar
23. The program below must: Display Lakers as the winners. Asks for points scored by Lakers and Celtics then calculate the difference and displays it on the screen in a full sentence.
Copy the code below, run it and fix the errors.
prinnt("The Lakers have won over the Celtics, please enter points scored"
nba_lakers = int(input"How many points did the lakers score?"))
nba_celtics = float(input("How many points did the celtics score?")
pointdifference = nba-lakers - nba-celtics
print("The Lakers won by",pointdifferences "points")
Paste the correct version below:
Created by Mr Suffar
24. The program below must: Display a welcome message, ask for benchpress weight and display it in a full sentence. Then ask for number of pullups and displays it in a full sentence. Then calculate and display the strength level using the following formula: strengthlevel = (benchpress*pullups) - weight
Copy the code below, run it and fix the errors.
print("Welcome to the gym)
benchpress = float(input("How much can you bench in KG")))
print("I can bench"benchpress,"KG")
pullups = intt(input("How many pullups can you perform?"))
print("I can do",pullup,"pullups")
weight = float(inrput("How much do you weight in KG?"))
strengthlevel = (benchpress*pullups - weight
print("Your strenght level is",strengthlevel
Paste the correct version below:
Created by Mr Suffar
25. The program below must store 365 in a variable called daysinyear and then ask the user for their age. Then display how many days, hours, minutes and seconds they lived.
daysinyear = ____
age = ____(_____("Enter your age"))
totaldays = daysinyear*____
print("You have lived",_____, "days")
hours = totaldays*____
print("You have lived",____, "hours")
_____ = hours*60
print("You have lived",_____, "minutes")
seconds = _____*____
print("You have lived",seconds, "______")
Copy the code above, fill the gaps then paste your code below:
Created by Mr Suffar
26. The program below must display a message to ask the user to calculate angle 3 in a triangle then asks for angle1 and angle2 then displays the value of angle 3 in a full sentence.
_____("The sum of the angles of a triangle is 180° ,calculate angle3")
angle1 = float(_____("Enter the value of angle 1"))
angle2 = _____(input("Enter the value of angle 1"))
angle3 = ____-_____-_____
print("Angle3 is "+str(_____)+"°")
Copy the code above, fill the gaps then paste your code below:
Created by Mr Suffar
27. 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:
Created by Mr Suffar
28. Create an algorithm to calculate the area of a rectangle. Your algorithm must:
Paste your code below:
Created by Mr Suffar
29. 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:
Created by Mr Suffar
30. Asks the user to enter 3 temperatures, calculate and display the average and total of the 3 temperatures.
Paste your code below:
Created by Mr Suffar
31. Create an algorithm that:
Paste your code below:
Created by Mr Suffar
Question 32
Answer:
Answer:
Answer:
Created by Mr Suffar
Question 33
✔
✔
Created by Mr Suffar
Question 34
?
?
Answer:
Created by Mr Suffar
Question 35
Answer:
Created by Mr Suffar
Question 36
?
?
?
Created by Mr Suffar
37. Create an algorithm that finds the perimeter rectangle.
Your program must:
Paste your code below:
Created by Mr Suffar
38. Create an algorithm that asks the user for a number, then displays helloooo, however the number of o’s in hello will be determined by the number the user enters. So if the user enters 6, then it should display helloooooo
Paste your code below:
Created by Mr Suffar
39. Create an algorithm that calculates the total points scored by a basketball team in a basketball match.
Create a program that:
Paste your code below:
Created by Mr Suffar
40. A farmer wants to count how many legs can be counted among all his animals.
The farmer has chickens = 2 legs, cows = 4 legs, pigs = 4 legs.
Create a program that:
Paste your code below:
Created by Mr Suffar
Homework: Watch the following video: https://youtu.be/hJ0FtnWFdOE
AND
Kahoot: https://create.kahoot.it/share/python-data-type-homework/38bac721-4318-4ccd-97aa-1099d046e074
Created by Mr Suffar
Revisit phase / starter:
Log on kahoot
https://create.kahoot.it/share/data-type-hard/0be31660-e005-42bb-94ba-7f4d6f9c53c3
Created by Mr Suffar
Arithmetic Operators
Lesson 3
Thursday, June 15, 2023
Lesson objectives…
Created by Mr Suffar
Arithmetic operators: https://youtu.be/hJ0FtnWFdOE
Created by Mr Suffar
Arithmetic Operators
Command | Name | Example | Output |
* | Multiplication | 6*5 | 30 |
/ | Division | 6/3 | 2 |
MOD (% in Python) | Modulus / modulo | 10 MOD 3 | 1 |
^ (** in Python) | Exponent | 3^2 | 9 |
DIV ( // in Python) | Integer Division | 10 DIV 3 | 3 |
Created by Mr Suffar
COLD CALL
Created by Mr Suffar
COLD CALL
3
1
Created by Mr Suffar
COLD CALL
Created by Mr Suffar
Consolidation
Created by Mr Suffar
Commenting
Created by Mr Suffar
ROUND function
Created by Mr Suffar
Apply your knowledge to tasks
Created by Mr Suffar
41. Complete the following table:
Question | Answer |
9 * 10 = |
|
20/4 = | |
print(round(5.7)) | |
print(round(6.87895 , 2)) | |
5 ** 2 = | |
5 % 2 = | |
5 // 2 = | |
Created by Mr Suffar
42. Copy the code below, run it and test it, then explain the purpose of the program/what the program does.
Explain the purpose of the code below:
icecream = int(input("How many icecreams do you want?"))
milkshake = int(input("How many milkshakes do you want?"))
iceprice = float(input("Enter the price of each icecream"))
milkprice = float(input("Enter the price of each milkshake"))
total = (icecream*iceprice) + (milkshake*milkprice)
print("The total amount that you have to pay is:",round(total))
Answer:
Created by Mr Suffar
43. Copy the code below, run it and test it, then explain the purpose of the program/what the program does.
Explain the purpose of the code below:
footballteam = int(input("How many players does your team currently have?"))
playersneeded = 11 % footballteam
print("You need:",playersneeded,"players before the match can start")
Answer:
Created by Mr Suffar
44. Copy the code below, run it and test it, then explain the purpose of the program/what the program does.
Explain the purpose of the code below:
students = int(input("How many students are in the class?"))
kpopfans = int(input("How many students listen to KPOP"))
percentage = kpopfans/students * 100
twodecimal = round(percentage,2)
print("Percentage of students who listen to KPOP is:",twodecimal,"%")
Answer:
Created by Mr Suffar
45. The program below must: Ask for number of people at a dinner table. Asks for the total bill. Asks for the percentage TIP to be added on top of the bill that the group want to pay. Calculates and display the total amount to be paid after the TIP is added. Calculates and displays how much each person will pay rounded to 2 decimal places.
Copy the code below, run it and fix the errors.
people = str(input("How many people are at the dinner table"))
bill = float(inrput("Enter the total bill for the food")
percentage = int(input("What percentage of the bill do you want to pay as a tip?"))
total = bills + ((bill/100) * percentage
payeach = total :) people
print("Total amount to be paid with TIP is:",nothing)
print("Each person will pay",round(payeach,8))
Paste the correct version below:
Created by Mr Suffar
46. A school teacher is organising a trip and wants to calculate the total cost of a trip and the total amount need to be paid by each student.
The program below must: Ask the user for cost of bus, hotel and activities. Ask for number of students. Calculate and displays the overall total cost of the trip. Calculates and dispays the amount each student needs to pay rounded to the nearest whole number.
Copy the code below, run it and fix the errors.
bus = float(input("How much does hiring the full bus cost")))
hotel = bool(input(("Enter total cost of all rooms needed"))
activities = float(inputt("Enter total cost of all activities"))
students = int(input("Enter number of students))
totalcost = bus+hotel+activitie
payeach = totalcost * students
print("Total overall cost of the trip,"totalcost)
print("Total cost to be paid by each student:",rounds(payeach))
Paste the correct version below:
Created by Mr Suffar
47. A basketball coach wants to share energy drinks evenly between his players.
Create a program that:
drinks = ____(____("How many energy drinks needed?"))
players = ____(____("How many players are in the team?"))
drinkeach = drinks // ____
drinksremain = ____ % players
print("Each player will receive:",____,"drink")
print("Drinks remaining:",____)
Copy the code above, fill the gaps then paste your code below:
Created by Mr Suffar
48.
Paste your code below:
Created by Mr Suffar
49. Create a program that:
Paste your code below:
Created by Mr Suffar
50. A pay to win Fifa player is buying Fifa coins to buy players. Players receive 105 Fifa coins per 1$.
Create a program that:
Paste your code below:
Created by Mr Suffar
51. Create a program that:
Paste your code below:
Created by Mr Suffar
52. Create a program that:
Paste your code below:
Created by Mr Suffar
53. Create a program that:
Paste your code below:
Created by Mr Suffar
54. 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:
Paste your code below:
Created by Mr Suffar
55. 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:
Created by Mr Suffar
56. A shop owner pays 20% tax on each item they sell. Create an algorithm that:
Paste your code below:
Created by Mr Suffar
57. A user wants to measure the length of his car. Create an algorithm that:
Paste your code below:
Created by Mr Suffar
58. The following formula is used to convert Celsius to Fahrenheit: Y°F =(X°C × 9/5) + 32
Your algorithm must:
Paste your code below:
Created by Mr Suffar
59. A shop wants to create an online payment system using gift cards only.
Create an algorithm that:
Paste your code below:
Created by Mr Suffar
60. 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:
Paste your code below:
Created by Mr Suffar
61. 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:
Tom also needs to fill the car with Diesel, the car uses approximately £0.5 in diesel per mile.
Create an algorithm that:
Paste your code below:
Created by Mr Suffar
62. 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:
Paste your code below:
Created by Mr Suffar
63. Create a program that calculates the area of a triangle.
Area of triangle formula: (base* height) / 2
Your algorithm must:
Paste your code below:
Created by Mr Suffar
64. In a tea shop, for each 6 tea cups bought by a customer, the customer gets 1 free. So if they pay for 12 cups, they get 14 in total.
Create a program that:
Paste your code below:
Created by Mr Suffar
65. This is a task for a game called “Among us the imposter game”.
Create a program that calculates the chances of being an imposter in a game. The formula to calculate the chances of being an imposter is: 100 x (i / p) where i is the number of imposters and p is the total number of all players.
Create a program that:
Paste your code below:
Created by Mr Suffar
Task 66
Paste your python code below:
Created by Mr Suffar
Watch the video
Then complete the kahoot:
https://create.kahoot.it/share/arithmetic-operators-homework/154634f3-b604-4e58-91fd-87407d316bf7
Homework
Created by Mr Suffar
1. State the values of a, b, c, d and e after the following operations are carried out:
(a) a = 26 MOD 5 =
(b) b = 50 DIV 7 =
(c) c = 10 ^ 3 =
(d) d = 10 MOD 5 =
(e) e = “4” + “56” =
Revisit phase / Starter
?
?
?
?
?
Created by Mr Suffar
If statement
Lesson 4
Thursday, June 15, 2023
Lesson objectives…
Created by Mr Suffar
Lesson theory
Created by Mr Suffar
Comparison Operators
Operator | Meaning | Example | True/False |
> | Greater than | 44 > 5 | |
< | Less than | 33<20 | |
== | Equal to | 10==20 | |
!= | Not equal to | 5 != 2 | |
>= | Greater than OR equal to | 20>=20 | |
<= | Less than OR equal to | 5<=10 | |
True
False
False
True
True
True
Created by Mr Suffar
Consolidation phase- Your turn
Operator | Meaning | Example | True/False |
> | Greater than | 2 > 5 | |
< | Less than | 23<20 | |
== | Equal to | 16==16 | |
!= | Not equal to | 7 != 7 | |
>= | Greater than OR equal to | 11>=20 | |
<= | Less than OR equal to | 30<=30 | |
False
False
True
False
False
True
Created by Mr Suffar
Go on: www.kahoot.it
https://create.kahoot.it/share/comparison-operators-python/4c284ca9-fd7a-4524-8753-b08c6896d230
Application phase – Apply what you have learnt!
Created by Mr Suffar
If statement
Then
If
Else
SELECTION: A programming construct that decides which statements are to be executed depending upon the result of a question/condition.
Created by Mr Suffar
Structure of an if statement
name = input("Enter a name")
if name == "Rose":
print("Hi Rose")
Variable
Asks the user to input a name
After writing if, you need a condition.
A condition must end with a COLON :
This space (TAB) represents Indentation which signifies the start and end of a block of code.
Created by Mr Suffar
Structure of an if statement
name = input("Enter a name")
if name == "Rose":
print("Hi Rose")
else:
print("Who are you")
Means OTHERWISE. It will display Who are you If anything other than Rose is entered.
Must end with a colon
Must be indented.
Created by Mr Suffar
Structure of an if statement
name = input("Enter a name")
if name == "Rose":
print("Hi Rose")
elif name == "Sarah":
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.
Remember to always put
" Quotation marks" around strings.
Created by Mr Suffar
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
Created by Mr Suffar
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
Created by Mr Suffar
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
Created by Mr Suffar
Structure of an if statement
powerlevel= int(input("enter power level"))
if powerlevel > 9000:
print("Over 9000")
elif powerlevel < 9000:
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
Note: DO NOT PUT "QUOTATION MARKS" AROUND NUMBERS.
Created by Mr Suffar
Nested if statement
socialmedia = input("Do you use social media?")
if socialmedia == "yes":
platform = input("Which platform do you use?")
if platform == "Facebook":
print("You are too old")
else:
print("Good choice")
else:
print("Good choice, social media is addictive")
Nested if statement
is an if statement inside another if statement.
Created by Mr Suffar
Application phase – Apply your knowledge
Created by Mr Suffar
Task 67 – 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 | |
Created by Mr Suffar
68. Copy the code below, run it and test it, then explain the purpose of the program/what the program does.
Explain the purpose of the code below:
youtubeceleb = input("Enter the name of a youtube celebrity")
if youtubeceleb == "mrbeast":
print("Correct")
else:
print("Try again")
Answer:
Created by Mr Suffar
69. Copy the code below, run it and test it, then explain the purpose of the program/what the program does.
Explain the purpose of the code below:
mark = int(input("Enter mark"))
if mark >=70:
print("Distinction")
elif mark >= 50:
print("Merit")
else:
print("Pass")
Answer:
Created by Mr Suffar
70. Copy the code below, run it and test it, then explain the purpose of the program/what the program does.
Explain the purpose of the code below:
binary = input("Would you like to enter a binary number?")
if binary == "yes":
binarynum = input("Enter a binary number")
else:
denary = input("Would you rather enter a denary number then?")
if denary == "yes":
denarynum = int(input("Enter a denary number"))
else:
print("You are boring")
Answer:
Created by Mr Suffar
71. Copy the code below, run it and test it, then explain the purpose of the program/what the program does.
Explain the purpose of the code below:
# Example purchase amount
purchase_amount = 150
# If statement to determine discount
if purchase_amount >= 1000:
discount = 0.2 # 20% discount
elif purchase_amount >= 500:
discount = 0.1 # 10% discount
elif purchase_amount >= 200:
discount = 0.05 # 5% discount
else:
discount = 0 # No discount
discount_amount = purchase_amount * discount
final_amount = purchase_amount - discount_amount
print("Purchase amount:", purchase_amount)
print("Discount:", discount_amount)
print("Final amount:", final_amount)
Answer:
Created by Mr Suffar
72. The program below must: Ask for a country name, display Pyramids if “Egypt” is entered, displays COLD! If “Iceland” is entered, displays Taj Mahal if “India” is entered. Display Accepted for any other choice.
Copy the code below, run it and fix the errors.
country = input("Enter a country name"
if country = "Egypt":
print("Pyramids")
elseif country == Iceland:
print("COLD!")
elif countrys == "India"
print(Taj Mahal")
else
prints("Accepted")
Paste the correct version below:
Created by Mr Suffar
73. The program below must: Ask the user for a score, then determines the grade.
Copy the code below, run it and fix the errors.
score = int(input("Enter the score"))
if score >= 90
grade = "A"
elif score => 80:
grade = "B"
elif score >= "70":
grade = "C"
elif score >= 60
grade = D
else:
grade = "F"
print("The grade is" grade)
Paste the correct version below:
Created by Mr Suffar
74. The program below must: ask for price and then set the category to Low/Medium/High/Premium depending on the price. It then displays the category. Low is below 10, medium is between 10 and 30, high is between 30 and 49. Premium is 50 or above.
Copy the code below, run it and fix the errors.
price = floa(input("Enter price"))
# Nested if statement
if price < 10
category = Low"
elseif price < 50:
if price < 30:
category "Medium"
else:
category = "High"
else:
category = "Premium
print("Category:" category)
Paste the correct version below:
Created by Mr Suffar
75. The program below must: Calculate if the year is a leap year. A leap year is a year that is (divisible by 4 but not divisible by 100) OR (Divisible by 4, 100 and 400).
Copy the code below, run it and fix the errors.
year = int(input("Enter a year")
# If statement to check leap year
if year % 4 == 0
if year % 100 == 0:
if year MOD 400 == 0:
print(year, "is a leap year")
else
print(year, "is not a leap year")
else:
print(year, "is a leap year")
else:
print(year "is not a leap year")
Paste the correct version below:
Created by Mr Suffar
76. Create a program that:
digit = _____("Enter a 3 digit pin number")
if digit == "____":
____("It's BOND")
____:
____("Boring")
Copy the code above, fill the gaps then paste your code below:
Created by Mr Suffar
77. Create a program that:
num1 = ____(____("Enter first number"))
num2 = ____(____("Enter second number"))
if num1 / ____ == num1 // num2:
____("Divisible")
else:
____("Not divisible")
Copy the code above, fill the gaps then paste your code below:
Created by Mr Suffar
78. Create a program that:
pictures = ___(___("How many pictures did you send?"))
texts = ___(___("How many texts did you send?"))
data = ___(___("How many MB of data have you used?"))
bill = (___*0.35) + (___*0.10) + ((data / 500) * 2.50)
print("Bill for the month:",round(___,2))
if bill >___:
___("You will be better on a contrat")
Copy the code above, fill the gaps then paste your code below:
Created by Mr Suffar
79. Create a program that: Asks for time of day in the following format: hours.minutes example: 18.30. It then displays whether its morning, afternoon or evening.
hour = ____(____("Enter time of day, E.G: 17.30"))
# Nested if statement
if ____ < 12:
time_of_day = "____"
else:
if hour < ____:
time_of_day = "Afternoon"
____:
time_of_day = "Evening"
print("Time of Day:", ____)
Copy the code above, fill the gaps then paste your code below:
Created by Mr Suffar
80. Create a program that:
Paste your code below:
Created by Mr Suffar
81. Create a program that:
Paste your code below:
Created by Mr Suffar
82. Create a program that:
Paste your code below:
Created by Mr Suffar
83. Create a program that:
Paste your code below:
Created by Mr Suffar
84. Create a program that:
Paste your code below:
Created by Mr Suffar
85. Create a program that:
Paste your code below:
Created by Mr Suffar
86. Create a program that:
Hint: Use nested if statement (an if statement inside another if statement)
Paste your code below:
Created by Mr Suffar
87. Create a program that:
Paste your code below:
Created by Mr Suffar
88) Create a program that:
Paste your code below:
Created by Mr Suffar
89) Create a program that:
Paste your code below:
Created by Mr Suffar
90) Create a program that:
Paste your code below:
Created by Mr Suffar
91) 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:
Paste your code below:
Created by Mr Suffar
92) Create a program that:
Paste your code below:
Created by Mr Suffar
93) Create a program that:
Paste your code below:
Created by Mr Suffar
94) Create a program that:
Paste your code below:
Created by Mr Suffar
95) Create a program that:
Paste your code below:
Created by Mr Suffar
96) Create a program that:
Paste your code below:
Created by Mr Suffar
97) A circle can fit inside a square if the diameter of the circle does not exceed the length of a square side.
Paste your code below:
Created by Mr Suffar
Task 98
Paste your python code below:
Created by Mr Suffar
Task 99
Answer:
Created by Mr Suffar
Homework
Watch the video:
Then complete the kahoot quiz:
https://create.kahoot.it/share/if-statement-part-1-homework/a69de9a1-9338-42bc-bb8c-0d48bbbe550f
Created by Mr Suffar
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")
Revisit phase - Starter
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?
Created by Mr Suffar
If statement part 2
Lesson 5
Thursday, June 15, 2023
Lesson objectives…
Created by Mr Suffar
Lesson theory video
Watch the video:
Created by Mr Suffar
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.
Created by Mr Suffar
AND OPERATOR
The AND operator will output True if both sides are true, otherwise it will be False.
For example:
Boolean expression | True/False | Reason |
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 |
Created by Mr Suffar
OR OPERATOR
The OR operator will output True if either or both sides are true, otherwise it will be False.
For example:
Boolean expression | True/False | Reason |
10 == 10 OR 7 < 12 | True | Both sides are 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 |
Created by Mr Suffar
NOT OPERATOR
The NOT operator reverses the statement, if it’s True it become False, if it’s False it becomes True.
For example:
Boolean expression | True/False | Reason |
NOT (20== 20) | False | 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 |
Created by Mr Suffar
COLD CALL
Boolean expression | True/False |
11 > 5 AND 7 == 3 | |
NOT(13 == 2) | |
11 <= 2 OR 8 != 5 | |
NOT(12 > 2 AND 5 < 1) | |
False
True
True
True
Created by Mr Suffar
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
Created by Mr Suffar
Consolidation
Created by Mr Suffar
name = input(“What is your name?”)
if name == "James" or name == "Don":
print("I know you!")
else:
print("Who are you?")
Example:
What will this program 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?
Created by Mr Suffar
We can use the AND operator to find if a number is between 2 specific numbers.
num = int(input("Enter a number"))
if num >=1 and num<=10:
print("Correct")
else:
print("Incorrect")
Range Check
What will this program display if the user enters the number 5?
Correct
Created by Mr Suffar
We can use the OR operator to find if a number DOES NOT fall between 2 specific numbers.
num = int(input("Enter a number"))
if num <1 or num>10:
print("Incorrect")
else:
print("Correct")
Range Check
What will this program display if the user enters the number 22?
Incorrect
Created by Mr Suffar
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.
Created by Mr Suffar
Application phase
Created by Mr Suffar
100. Copy the code below, run it and test it, then explain the purpose of the program/what the program does.
Explain the purpose of the code below:
side1= int(input("Enter the length 1"))
side2= int(input("Enter the length 2"))
side3= int(input("Enter the length 3"))
# If statement to determine the type of triangle
if side1 == side2 and side2== side3:
triangle_type = "Equilateral"
elif side1 == side2 or side1 == side3 or side2 == side3:
triangle_type = "Isosceles"
else:
triangle_type = "Scalene"
print("The triangle is", triangle_type)
Answer:
Created by Mr Suffar
101. Copy the code below, run it and test it, then explain the purpose of the program/what the program does.
Explain the purpose of the code below:
purchase_amount = float(input("Enter purchase amount"))
membership_level = input("Enter membership level")
# Nested if statement with logical operators
if purchase_amount >= 100 and membership_level == "Gold":
discount = 0.2
elif purchase_amount >= 50 or membership_level == "Silver":
discount = 0.1
else:
discount = 0
final_amount = purchase_amount - (purchase_amount * discount)
print("Purchase Amount:", purchase_amount)
print("Membership Level:", membership_level)
print("Discount Percentage:", discount * 100, "%")
Answer:
Created by Mr Suffar
102. Copy the code below, run it and test it, then explain the purpose of the program/what the program does.
Explain the purpose of the code below:
age = int(input("Enter your age"))
is_student = input("Are you a student? True or False")
if age < 5 or age >= 60:
print("Free ticket")
else:
if is_student== True:
print("Student ticket: $10")
else:
if age >= 18:
print("Adult ticket: $15")
else:
print("Child ticket: $8")
Answer:
Created by Mr Suffar
103. The program below must: Ask for value of angle, displays Acute if the angle is less than 90. Display Right if the angle is 90. Display Obtuse if the angle is between 91 and 179. Otherwise display Straight.
Copy the code below, run it and fix the errors.
angle = int(input("Enter angle")
# If statement to determine angle type
if angle < 90
angle_type = "Acute"
elif angle = 90:
angle_type = "Right"
elif angle > 90 or angle < 180:
angle_type = "Obtuse"
else
angle_type = "Straight"
print("The angle is" angle_type)
Paste the correct version below:
Created by Mr Suffar
104. The program below must: Ask the user for age, display whether the user is a child, adult or senior depending on their age. If the user is less than 18 they are a child, if the user is between 18 and 64, they are an Adult. Otherwise they are Senior.
Copy the code below, run it and fix the errors.
age = str(input("Enter your age"))
# If statement to determine age category
if age > 18:
category = "Child
elif age >= 18 or age < 65:
category = "Adult"
else
category = "Senior"
print("The age category is" category)
Paste the correct version below:
Created by Mr Suffar
105. The program below must: The program must take 2 inputs, grade and income, representing the student's grade and annual income, respectively. The if statement checks the grade and income against specific criteria to determine the scholarship eligibility. If the grade is 90 or above and the income is less than $60,000, the student is eligible for a full scholarship. If the grade is 80 or above and the income is less than $80,000, the student is eligible for a partial scholarship. If the criteria for either scholarship is not met, the student is not eligible for a scholarship. Finally, it prints the scholarship eligibility status.
Copy the code below, run it and fix the errors.
grade = int(inrput("Enter grade"))
income = int(input("Enter income")
# If statement to determine scholarship eligibility
if grade >= 90 and income < 60000
eligibility = "Eligible for Full Scholarship"
elif grade >= 80 or income < 80000:
eligibility = "Eligible for Partial Scholarship"
else
eligibility = "Not Eligible for Scholarship"
print("Scholarship eligibility:", eligibilityyy)
Paste the corrected version below
Created by Mr Suffar
106. Create a program that:
age = ____(____("Enter your age: "))
# Check if the age is between 18 and 30 (inclusive)
if age >= 18 ____ age <= 30:
____("You are eligible for this program.")
____:
____("You are not eligible for this program.")
Copy the code above, fill the gaps then paste your code below:
Created by Mr Suffar
107. Create a program that:
num = __(____("Enter a number: "))
# Check if the number is even and positive
if num % 2 == __ and num > __:
_____("The number is even and positive.")
_____:
_____("The number is not even and positive.")
Copy the code above, fill the gaps then paste your code below:
Created by Mr Suffar
108. Create a program that:
rating = int(input("On a scale of 1 to 10, how happy do you feel? "))
# Check happiness rating and display corresponding message
if rating <= ___:
print("I'm sorry to hear that you're feeling low.")
___ happiness_rating >= 4 ___ happiness_rating <= 7:
print("It sounds like you're doing okay. Stay positive.")
___:
___("That's wonderful to hear!")
Copy the code above, fill the gaps then paste your code below:
Created by Mr Suffar
109) Create a program that:
Paste your code below:
Created by Mr Suffar
110) Create a program that:
Paste your code below:
Created by Mr Suffar
111) Create a program that:
Paste your code below:
Created by Mr Suffar
112) Create a program that:
Paste your code below:
Created by Mr Suffar
113) Write a program that:
Paste your code on the below:
Created by Mr Suffar
114) Write a program that:
Paste your code on the below:
Created by Mr Suffar
Task 115
Answer:
Answer:
Created by Mr Suffar
Task 116
Paste your code below:
Created by Mr Suffar
Task 117
Paste your code below:
Created by Mr Suffar
Task 118
Paste your code below:
Created by Mr Suffar
119) Create a program that allows the user to convert between different units:
There are 8 bits in 1 bye, and 1000 bytes in 1 Kilobytes.
Paste your code on the below:
Created by Mr Suffar
Homework
Watch the video:
Then complete the kahoot:
Created by Mr Suffar
If statement part 3
Lesson 6
Thursday, June 15, 2023
Lesson objectives…
Created by Mr Suffar
Lesson theory
Watch the video:
Created by Mr Suffar
Syntax error
Syntax Error: When you break the rules of the programming language.
Examples:
Created by Mr Suffar
Cold call – Find the syntax errors
Spot the syntax errors in the code
Should be input
Missing colon :
Missing “”
Missing parenthesis )
Created by Mr Suffar
Logic Error
Logic Error: When your program runs however it produces an unexpected results.
Examples:
Created by Mr Suffar
Cold call – Find the logic errors
Spot the logic errors in the code
Should be and
Should be <
Created by Mr Suffar
Cold call – Find the logic errors
Spot the logic errors in the code
Should be <= because the user could have the exact amount of mealcost.
Created by Mr Suffar
COLD CALL – FINDING ERRORS
Identify the errors and whether they are syntax or logic
Syntax error: Double
Brackets missing ))
Logic error: must be > instead of <
Logic error: must be < instead of >
Syntax error: Missing Quotation mark "
Created by Mr Suffar
Tasks
Complete tasks
Created by Mr Suffar
120. Copy the code below, run it and test it, then explain the purpose of the program/what the program does.
Explain the purpose of the code below:
number = int(input("Enter a number"))
if number % 3 == 0 and number % 5 == 0:
print(number, "is divisible by both 3 and 5")
else:
print(number, "is not divisible by both 3 and 5")
Answer:
Created by Mr Suffar
121. The program below must: Ask the user for a positive number, if the number is positive, it displays positive and whether it is even or odd. If its not positive, it displays not a positive number.
Copy the code below, run it and fix the errors.
num = int(input"Enter a positive number"))
# Nested if statement
if num < 0:
print("Number is positive")
if num % 2 == 0
print("Number is even")
else
print("Number is odd"
else:
print("Number is not positive)
Paste the correct version below:
Created by Mr Suffar
122. Create a program that: Calculates the number of weeks and days from a given number of days then displays it on the screen in a full sentence.
days = ____(____("Enter the number of days"))
# Integer division and remainder calculation
weeks = days // ___
remaining_days = days __ 7
print("Weeks:", ____)
print("Remaining Days:", ________)
Copy the code above, fill the gaps then paste your code below:
Created by Mr Suffar
123. Ask the user for a number.
Display if the number is odd or even.
Hint: Use MODULO %
Paste your code below:
Created by Mr Suffar
124) 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:
Paste your code below:
Created by Mr Suffar
125) Create a program that:
Paste your code below:
Created by Mr Suffar
126) Create a program that: Asks the user to enter a decimal number. Round the decimal number to the nearest whole number then display the number and also display whether that number is even or odd.
Paste your code below:
Created by Mr Suffar
127) Create a program that:
Paste your code below:
Created by Mr Suffar
128) 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:
received the last attack (out of 500)
Paste your code below:
Created by Mr Suffar
129) Code the following flowchart in python:
Yes
No
No
Paste your code below:
Created by Mr Suffar
130) Code the following flowchart in python:
Paste your code below:
Created by Mr Suffar
131) Tom wants to hire a car. He wants to know how much it will cost him to drive to a different city and back.
Create an algorithm that:
Paste your code below:
Created by Mr Suffar
Task 132
Paste your code below:
Created by Mr Suffar
Task 133
?
?
?
?
?
Created by Mr Suffar
Task 134
Paste your code below:
Created by Mr Suffar
Task 135
Paste your code below:
Created by Mr Suffar
Task 136
Paste your code below:
Created by Mr Suffar
Task 137
Paste your code below:
Created by Mr Suffar
Task 138
Paste your code below:
Created by Mr Suffar
Task 139
Paste your code below:
Created by Mr Suffar
Task 140
Paste your code below:
Created by Mr Suffar
Task 141
Paste your code below:
Created by Mr Suffar
Task 142
?
?
Created by Mr Suffar
Task 143
Paste your code below:
Created by Mr Suffar
Task 144
Paste your code below:
Created by Mr Suffar
Task 145
Paste your code below:
Created by Mr Suffar
Task 146
Paste your code below:
Created by Mr Suffar
Task 147
Paste your code below:
Created by Mr Suffar
Homework
Created by Mr Suffar
MS Form assessment:
End of unit test
Created by Mr Suffar