These questions follow Conditionals. A conditional is a fork in the road β the whole skill is knowing which branch runs, and why.
Questions
- Which of these jobs needs a conditional, and which is just a sequence of steps? (a) greet every name on a list, (b) charge less if the customer is a student, (c) print todayβs date.
- Predict the output when
tempis-5, then when it is30.if temp > 25: print("Hot") elif temp > 0: print("Mild") else: print("Brrr") - Find the bug. Python refuses to run
if mark = 50:at all. What did the writer mean, and why does Python object? - Predict the output when
ageis16β then when it is20.if age >= 13: if age <= 19: print("Teen") - Write a four-line snippet: ask for a password, then print
Welcomeif it equalssesameandNo entryotherwise. - Challenge. Rewrite question 4 as a single
ifusingandβ then suggest why real checks often keep conditions separate.
Answers
Answer 1
Only (b) β the price depends on something. (a) is repetition β a job for a loop β and (c) is plain sequence.
Answer 2
-5: neither test is true βBrrr.30:Hot, andMildnever gets a look β anelifchain stops at the first true test.
Answer 3
They meant
==, the question βare these equal?β. A single=is a command, and a command cannot be a condition β aSyntaxError.
Answer 4
16: both tests pass βTeen.20: the outer test passes, the inner one fails, and nothing at all prints β there is noelse.
Answer 5
password = input("Password: ");if password == "sesame":withprint("Welcome")beneath;else:withprint("No entry").
Answer 6
if age >= 13 and age <= 19:β one line, same behaviour. Separate conditions let each failure carry its own, more useful message.
Curriculum connection
C1.4
determine the appropriate expressions and instructions to use in a programming statement, taking into account the order of operations
Link to original
C1.5
identify and explain situations in which conditional and repeating structures are required
Link to original
C2.3
write programs that include single and nested conditional statements
Link to original