Programming with Python · free preview
Making Choices
Write conditional statements including `if`, `elif`, and `else` branches. Correctly evaluate expressions containing `and` and `or`.
1. Overview
Overview
2. Conditionals
Conditionals
3. Comparing in Python
Comparing in Python
4. `True` and `False`
True and False
5. Checking our Data
Checking our Data
6. How Many Paths?
How Many Paths?
7. What Is Truth?
What Is Truth?
8. That's Not Not What I Meant
That's Not Not What I Meant
9. Close Enough
Close Enough
10. In-Place Operators
In-Place Operators
11. Sorting a List Into Buckets
Sorting a List Into Buckets
12. Counting Vowels
Counting Vowels
13. Pattern matching with match and case
Pattern matching with match and case
14. Identify all truthy values
Which option contains only truthy Python values?
15. Tracing a three-branch temperature classifier
Trace this complete Python program for the three inputs 39.2, 38.5, and 37.8. For each input, state which test is evaluated first, which branch runs, and the exact printed line.
Example. Tracing a three-branch temperature classifier
Problem. Trace this complete Python program for the three inputs 39.2, 38.5, and 37.8. For each input, state which test is evaluated first, which branch runs, and the exact printed line.
Correct solution.
def classify(temperature):
if temperature >= 39.0:
return 'high'
elif temperature >= 38.0:
return 'low-grade'
else:
return 'normal'
for temperature in [39.2, 38.5, 37.8]:
print(temperature, classify(temperature))Expected input list,[39.2, 38.5, 37.8].
Expected output.``output 39.2 high 38.5 low-grade 37.8 normal ``
Trace/reasoning, for 39.2, 39.2 >= 39.0 is true, so classify returns 'high' and the later tests are not evaluated. For 38.5, the first test 38.5 >= 39.0 is false and the second 38.5 >= 38.0 is true, so it returns 'low-grade'. For 37.8, both tests are false, so the else branch returns 'normal'. The branch order matters, placing the >= 38.0 test first would incorrectly label 39.2 as 'low-grade'.
关键提醒。
The program demonstrates a complete if / elif / else chain with a return value, three distinct branch outcomes, and an explicit prediction for each test result.
16. Retrieval practice
Without looking back, write a complete Python conditional for a body temperature of 38.2, return high when the temperature is at least 39.0, return low-grade when it is at least 38.0, and otherwise return normal. Predict the exact output line.
Write the full branch chain, not just the tests, and state which branch runs for 38.2.
17. Mastery check
Apply the lesson to a new diagnosis input, write classify(max_day0, max_day20). It returns suspicious when max_day0 == 0 and max_day20 == 20; otherwise it returns high when max_day20 > 20; otherwise normal when max_day20 >= 10; otherwise low. Predict the outputs for (0, 20), (5, 21), (5, 12), and (5, 5).
Write the complete function and the four exact outputs, one input tuple per line.
Practice this interactively
Free account · instant grading · spaced review that schedules itself.
Start this course for free