๐ Python from scratch ยท Session 4
Making decisions: if, elif and else: practice sheet
Part 1: Questions with True or False answers
Remember: A comparison gives True or False. == asks "equal?", != "not equal?", and < > <= >= compare sizes.
1. Warm-up x = 15. Print whether x is greater than 10, then whether x equals 15.
x = 15
2. Core a = 7 and b = 7. Print whether they are equal, then whether they are different.
a = 7 b = 7
3. Stretch ๐ฅ n = 24. Print whether n is even, then whether n is a multiple of 3.
n = 24
Part 2: if and else
Remember: if condition: runs the indented lines only when the condition is True; else: runs when it is False.
4. Warm-up temp = 30. Print Hot if temp is above 25, otherwise Not hot.
temp = 30
5. Core n = 7. Print even or odd.
n = 7
6. Stretch ๐ฅ pin = 4821. Print Access granted if pin is 4821, otherwise Wrong PIN, then always print Goodbye.
pin = 4821
Part 3: Many choices with elif
Remember: elif adds more cases between if and else. Python checks from the top and runs only the first True one.
7. Warm-up speed = 45. Print Slow below 30, Normal below 60, otherwise Fast.
speed = 45
8. Core n = -4. Print positive, negative or zero.
n = -4
9. Stretch ๐ฅ Ticket price by age: under 5 free (Price: 0), under 13 Price: 100, 60 or over Price: 120, everyone else Price: 200.
age = 30
Part 4: and, or, not
Remember: and is True only when both sides are True; or when at least one is; not flips True and False.
10. Warm-up day = "Sunday". Print Holiday! on Saturday or Sunday, otherwise School day.
day = "Sunday"
11. Core Print Ride if height is at least 120 and age at least 8, otherwise No ride.
height = 130 age = 9
12. Stretch ๐ฅ Leap year, full rule: divisible by 4 and not by 100, or divisible by 400. Print leap or not leap.
y = 2024
Activity 1: The grade machine
13. Print Grade: A for 90 or more, B for 75 to 89, C for 50 to 74 and D below 50. It will be tested with many marks.
- Write if marks >= 90: and print Grade: A.
- Add elif for B (75 or more).
- Add elif for C (50 or more).
- Add else for D.
- Test 90, 75, 50 and 49 yourself.
marks = 82 # Step 1 to 4
Activity 2: Electricity bill
14. The first 100 units cost 5 each, the next 100 cost 7 each and every unit above 200 costs 10. Print Bill: and the amount. Test with 80, 150, 200 and 250 units.
- If units <= 100: bill = units ร 5.
- elif units <= 200: bill = 500 + (units โ 100) ร 7.
- else: bill = 500 + 700 + (units โ 200) ร 10.
- Print Bill: and the bill.
units = 150 # Steps 1 to 3: work out bill # Step 4
Activity 3: Rock, paper, scissors judge
15. p1 and p2 hold rock, paper or scissors. Print Draw, Player 1 wins or Player 2 wins. Rock beats scissors, scissors beat paper, paper beats rock.
- If p1 == p2 print Draw.
- elif p1 beats p2 (three cases joined with or) print Player 1 wins.
- else print Player 2 wins.
- Test all nine pairs if you can.
p1 = "rock" p2 = "scissors" # Step 1 # Step 2 # Step 3
Predict the output
Work it out on paper first, then type it into the lesson page to check.
16. What does this print?
print(5 > 3)
17. What does this print?
x = 7 if x % 2 == 0: print("even") else: print("odd")18. What does this print?
t = 20 if t > 30: print("hot") elif t > 15: print("warm") else: print("cold")19. What does this print?
print(True and False)
20. What does this print?
print(not (3 == 3))
21. What does this print?
age = 15 if age >= 13 or age == 9: print("yes") else: print("no")22. What does this print?
n = 12 r = "" if n > 10: r = r + "A" if n > 5: r = r + "B" print(r)
Homework
23. Warm-up year = 2024. Print leap if it is divisible by 4, else not leap (simple rule).
year = 2024
24. Core a, b and c are three numbers. Print the largest.
a = 4 b = 9 c = 2
25. Stretch ๐ฅ Three sides of a triangle. Print equilateral, isosceles or scalene (or not a triangle if one side is at least the sum of the other two).
a = 3 b = 4 c = 5
Answer key
x = 15 print(x > 10) print(x == 15)
a = 7 b = 7 print(a == b) print(a != b)
n = 24 print(n % 2 == 0) print(n % 3 == 0)
temp = 30 if temp > 25: print("Hot") else: print("Not hot")n = 7 if n % 2 == 0: print("even") else: print("odd")pin = 4821 if pin == 4821: print("Access granted") else: print("Wrong PIN") print("Goodbye")speed = 45 if speed < 30: print("Slow") elif speed < 60: print("Normal") else: print("Fast")n = -4 if n > 0: print("positive") elif n < 0: print("negative") else: print("zero")age = 30 if age < 5: print("Price: 0") elif age < 13: print("Price: 100") elif age >= 60: print("Price: 120") else: print("Price: 200")day = "Sunday" if day == "Saturday" or day == "Sunday": print("Holiday!") else: print("School day")height = 130 age = 9 if height >= 120 and age >= 8: print("Ride") else: print("No ride")y = 2024 if (y % 4 == 0 and y % 100 != 0) or y % 400 == 0: print("leap") else: print("not leap")marks = 82 if marks >= 90: print("Grade: A") elif marks >= 75: print("Grade: B") elif marks >= 50: print("Grade: C") else: print("Grade: D")units = 150 if units <= 100: bill = units * 5 elif units <= 200: bill = 500 + (units - 100) * 7 else: bill = 500 + 700 + (units - 200) * 10 print("Bill:", bill)p1 = "rock" p2 = "scissors" if p1 == p2: print("Draw") elif (p1 == "rock" and p2 == "scissors") or (p1 == "scissors" and p2 == "paper") or (p1 == "paper" and p2 == "rock"): print("Player 1 wins") else: print("Player 2 wins")- True
- odd
- warm
- False
- False
- yes
- AB
year = 2024 if year % 4 == 0: print("leap") else: print("not leap")a = 4 b = 9 c = 2 if a >= b and a >= c: print(a) elif b >= c: print(b) else: print(c)a = 3 b = 4 c = 5 if a >= b + c or b >= a + c or c >= a + b: print("not a triangle") elif a == b and b == c: print("equilateral") elif a == b or b == c or a == c: print("isosceles") else: print("scalene")