Year 8 · Spring 2
Python I · Lesson 3 of 5
if / elif / else
PracticalTopic 13 Programming basics
Year 8 · Python I · Lesson 3 of 5
You will learn to
- Use the comparison operators ==, !=, >, <, >= and <=.
- Use if, elif and else, with a colon and indentation, to choose one path.
- Explain why the order of the conditions matters.
- Use PRIMM to predict the output of a program before you run it.
Year 8 · Python I · Lesson 3 of 5
Success criteria
You have got it when you can say:
- I can write a grade program: 90 and over is an A, and so on down.
- I can predict the output of five short programs before I run them.
- I can explain what goes wrong if the lowest grade is checked first.
Year 8 · Python I · Lesson 3 of 5
Retrieval starter
Questions from before. Write your answers, then show them.
- Last lesson
What does 17 % 5 give?
A 2B 3.4C 3
A. 2
- Last unit
How many characters can 7-bit ASCII represent?
A 256B 128C 127
B. 128
- Earlier this year
count = 5, then while count > 0: print(count). What happens?
A Prints 5 for everB Prints nothingC Prints 5 4 3 2 1
A. Prints 5 for ever
Year 8 · Python I · Lesson 3 of 5
Key words (1 of 2)
- Variable
- A named place in memory that holds a value.
- Assignment
- Storing a value in a variable. = in Python, ← in pseudocode.
- Casting
- Changing a value from one data type to another, like int("13").
- Concatenation
- Joining strings together: "7" + "7" = "77".
Year 8 · Python I · Lesson 3 of 5
Key words (2 of 2)
- Selection
- Choosing a path with if / else.
- Iteration
- Repeating steps: a loop.
- Boolean
- A value that is only True or False.
- Infinite loop
- A loop whose condition never becomes false.
The explanation · step 1 of 1 · part 1 of 2
Selection: if, elif, else
score = int(input("Score out of 50: "))
if score >= 40:
print("Gold badge!")
elif score >= 25:
print("Bronze badge")
else:
print("Try the Respawn quest")- Check the
if. True? Run its lines and skip the rest. - False? Check the
elif. - Nothing true? Run the
else.
The explanation · step 1 of 1 · part 2 of 2
Selection: if, elif, else (continued)
Order matters. If >= 25 came first, a score of 45 would wrongly get Bronze.
Indentation (4 spaces) shows which lines are inside the if. The colon : is required.
Year 8 · Python I · Lesson 3 of 5
Quick quiz · 1 of 3
range(3) gives…
- A 1, 2, 3
- B 0, 1, 2
- C 0, 1, 2, 3
B. 0, 1, 2
Starts at 0, stops before 3.
Year 8 · Python I · Lesson 3 of 5
Quick quiz · 2 of 3
x = input("Number: ") and the user types 5. What type is x?
- A str
- B float
- C int
A. str
input() always gives a string: "5". Cast it with int() to do maths.
Year 8 · Python I · Lesson 3 of 5
Quick quiz · 3 of 3
How many times does for i in range(2, 10, 2): run?
- A 4
- B 8
- C 5
A. 4
2, 4, 6, 8. The next would be 10, which is the stop, so it isn't used. 4 times.
Year 8 · Python I · Lesson 3 of 5
Exit ticket
Answer on paper before you go. Then check.
score = 45. Code: if score >= 25: print("Bronze") elif score >= 40: print("Gold"). What prints?
A GoldB BothC Bronze
C. Bronze 45 >= 25 is True, so the first branch runs and the rest is skipped. The order is wrong: a logic error.
A program runs with no message but says the average of 4 and 6 is 7. This is a…
A Logic errorB Syntax errorC Runtime error
A. Logic error It ran but gave the wrong answer: a logic error. (4 + 6 / 2 = 7; it needs brackets.)