Python · 1.4
Selection: if, elif and else
Name
Class
Date
Mark / 31
Section A
Circle the letter of the correct answer. One mark each.
- 1What is wrong with
if score = 10:?1- AA comparison needs two equals signs
- BThere should be no colon
- CNothing is wrong
- Dscore must be in quotes
- 2A program has
if mark >= 40beforeelif mark >= 70. What does a mark of 90 print?1- ABoth messages
- BAn error
- CThe message for 70
- DThe message for 40, because the first true condition runs
- 3When is
age >= 13 and age <= 19true?1- AWhen age is above 13 or below 19
- BAlways
- CWhen age is between 13 and 19 inclusive
- DWhen age is 13 or 19 only
- 4What does
not Trueevaluate to?1- AAn error
- BFalse
- C0
- DTrue
- 5With
temperature = 30, how many lines does three separateifstatements print, if all three conditions are true?1- AOne
- BThree
- CAn error
- DNone
- 6What does
elsetake as its condition?1- ANothing, it runs when no condition above was true
- BAny condition you like
- CThe opposite of the if
- DThe same condition as the if
- 7Which is equivalent to
age >= 13 and age <= 19?1- A13 < age < 19
- Bage in (13, 19)
- C13 <= age <= 19
- Dage == 13 or age == 19
- 8What decides which lines belong inside an if branch?1
- AThe semicolons
- BThe brackets
- CThe indentation
- DThe order they were typed
- 9In a match statement, what does
case _:do?1- ARuns when nothing else matched
- BEnds the program
- CRuns first
- DRuns always
- 10A condition uses
if mark > 40for a pass. What happens to a mark of exactly 40?1- AIt causes an error
- BIt is ignored
- CIt passes
- DIt fails, because 40 is not greater than 40
Section B
Answer in the space provided. The number of marks tells you how many separate points to make.
- 1WriteWrite a program that asks for a mark out of 100 and prints Distinction for 70 or above, Merit for 50 to 69, Pass for 40 to 49 and Fail below 40.5
- 2ExplainExplain the difference between using an elif chain and using several separate if statements.4
- 3ExplainA program checks exam marks using
if mark >= 40followed byelif mark >= 70. Explain why no student is ever awarded a distinction, and state the correction.4 - 4WriteWrite a program that asks the user for their age, checks that what they typed is a whole number, and then states whether they may vote at 18 or not.5
- 5ExplainExplain why combining two conditions with
andis often preferable to nesting one if statement inside another.3
Mark scheme
Section A
Section B
1 Write a program that asks for a mark out of 100 and prints Distinction for 70 or above, Merit for 50 to 69, Pass for 40 to 49 and Fail below 40. 5
One mark for each of the following, to a maximum of 5.
- Reads and converts the input
- Tests for 70 or above first
- Uses elif for the 50 boundary
- Uses elif for the 40 boundary
- Uses else for everything below 40
Model answer. mark = int(input("Enter the mark: ")) if mark >= 70: print("Distinction") elif mark >= 50: print("Merit") elif mark >= 40: print("Pass") else: print("Fail") The conditions are ordered from most restrictive to least, which is essential. Python checks each condition in turn and runs the first one that is true, skipping the rest, so a mark of 85 is correctly caught by the first branch. If the 40 test came first, every mark of 40 or above would print Pass and the higher grades would never be reached.
2 Explain the difference between using an elif chain and using several separate if statements. 4
One mark for each of the following, to a maximum of 4.
- An elif chain is a single statement
- Only the first true branch runs and the rest are skipped
- Separate ifs are each judged independently
- So several separate ifs may all run
Model answer. An if, elif and else chain forms one single selection statement with several possible outcomes. Python tests each condition from the top and runs the block belonging to the first one that is true, then skips every remaining branch entirely, so exactly one branch runs. Several separate if statements are instead judged one after another and completely independently of each other, so if more than one of their conditions happens to be true then more than one block will run. This is why a temperature of 30 prints only Hot from an elif chain, but would print Hot, Mild and Not freezing from three separate ifs.
3 A program checks exam marks using if mark >= 40 followed by elif mark >= 70. Explain why no student is ever awarded a distinction, and state the correction. 4
One mark for each of the following, to a maximum of 4.
- Python checks conditions from the top and stops at the first true one
- Any mark of 70 or more also satisfies the 40 condition
- So the 40 branch always runs first and the elif is never reached
- Reorder so the most restrictive condition is tested first
Model answer. Python evaluates the conditions of a selection statement in the order they are written and runs the block belonging to the first condition that is true, skipping all the others. A mark of 85 satisfies mark >= 40 just as much as it satisfies mark >= 70, so the first branch is taken and the elif below it is never even tested. Every student scoring 40 or above therefore receives the same message. The correction is to order the conditions from most restrictive to least, testing mark >= 70 first, then mark >= 50, then mark >= 40, so that each mark is caught by the highest band it qualifies for.
4 Write a program that asks the user for their age, checks that what they typed is a whole number, and then states whether they may vote at 18 or not. 5
One mark for each of the following, to a maximum of 5.
- Reads the input as a string first
- Checks the input is numeric before converting
- Reports the problem if it is not numeric
- Converts the valid input to an integer
- Uses selection to print the correct voting message
Model answer. reply = input("Enter your age: ") if not reply.isdigit(): print("That is not a whole number.") else: age = int(reply) if age >= 18: print("You may vote.") else: print("Not yet.") The input is read as a string and checked with isdigit before any conversion is attempted, so int is only ever given something it can actually convert. Without that check, typing a word would raise a ValueError and stop the program. Validating first means the program explains the problem to the user instead of crashing, which is what any real program is expected to do.
5 Explain why combining two conditions with and is often preferable to nesting one if statement inside another. 3
One mark for each of the following, to a maximum of 3.
- Nesting adds a level of indentation for each condition
- Deeply nested code is harder to read and follow
- A single condition joined with and expresses the same logic in one level
Model answer. Each nested if adds another level of indentation, and code that is three or four levels deep becomes genuinely difficult to read because the reader has to hold every enclosing condition in their head at once to know when a line will actually run. Joining the conditions with and expresses exactly the same requirement, that both must be true, while keeping the code at a single level of indentation. The nested form is only preferable when the two conditions need separate else branches, because then each if genuinely has its own alternative to handle.