Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Objectives: LO-3.1, LO-3.2, LO-3.3
Prerequisites: the Module 3 reading and Lab 2
Time: about 2 hours

In this lab you predict the value of conditions, write scripts that choose what to print, repair two decisions that go wrong without any error message, and write tests that find the mistakes hiding at a decision’s boundaries. As before, each exercise ends with a check cell that prints passed or says what is wrong, and fails until you answer.

Setup

Run the next cell first. It prints the version of Python, makes the scratch folder, and defines Lab 2’s helpers: run_script(path, typed=...) runs a script with the text a user would type and shows its output, and output_of(path, typed=...) returns that output for a check.

1. Comparisons and Boolean expressions

Run the next cell, then change the two strings and run it again. Try "apple" and "Apple", then "10" and "9", then "cat" and "cats".

Exercise L3.1 (LO-3.1, ★, about 10 minutes)

Predict whether each expression is True or False, without running it, and write True or False (no quotes) after each name.

NameExpression
b17 > 3 and 2 > 5
b2not 7 > 3 or 2 < 5
b33 < 5 < 4
b4"Zebra" < "apple"
b510 == 10.0
b6"10" == 10
b75 >= 5 and not 5 > 5
b80.1 + 0.2 == 0.3
Hint

not applies only to the comparison right after it, because comparisons run first. Capital letters come before lowercase ones.

Exercise L3.2 (LO-3.1, ★, about 10 minutes)

For each value, predict whether Python treats it as true or as false in an if. Write True if it counts as true, and False if it counts as false.

NameValue
t10
t2"0"
t3""
t4" "
t5-1
t60.0
t7"False"
t8None

Check any you got wrong with bool, in the next cell: change the value and run it.

2. if, else, and elif

Run the next cell. Then change temperature to 30, 29, 20, 19, 10, 9, and -5, predicting the word before each run. Which values sit on a boundary?

Exercise L3.3 (LO-3.2, ★, about 15 minutes)

Write a script, scratch/parity.py, that asks for a whole number with the prompt Number: and says whether it is even or odd. With 7 typed, it prints:

Number: 7
7 is odd

The check also types 8, 0, and -3.

Hint

A number is even when the remainder after dividing it by 2 is 0.

Exercise L3.4 (LO-3.2, ★★, about 15 minutes)

A course grades a score out of 100: 85 or more is a distinction, 70 to 84 a merit, 50 to 69 a pass, and below 50 a fail. The script below is meant to print the grade, but it prints Pass for a score of 90. Run it with a few scores, work out why, and fix it, without adding and or chained comparisons: change only the order and the conditions.

Hint

Which condition is true for 90? Python runs the first true branch and skips the rest.

Exercise L3.5 (LO-3.1, LO-3.2, ★★, about 10 minutes)

This script should continue only when the user types yes or y. Run it with no typed: it continues anyway. Fix the condition.

3. Testing a decision

A cinema charges 5.00 for a child under 13, 9.50 for an adult from 13 to 64, and 6.00 for a senior of 65 or over. The next cell saves a script that prints the right ticket.

Exercise L3.6 (LO-3.3, ★, about 5 minutes)

The ticket changes at two boundaries. Name the last age on each side of each one.

The next cell defines a helper for testing the script. ticket_for(30) runs the script named by TICKET_SCRIPT with 30 typed, and returns the line it printed, so one test is one assert:

assert ticket_for(30) == "Adult ticket: 9.50"

Exercise L3.7 (LO-3.3, ★★, about 20 minutes)

Write tests for the ticket script in the next cell: one assert per test, each comparing ticket_for(...) with the line you expect. Reach every branch, and test both sides of both boundaries. One test is there to start you off. Run the cell: every test should pass, because the script is right.

Then run the check straight after your test cell. It runs your tests again on four versions of the script, each with one mistake at a boundary, and passes only if your tests catch all four. A test that can’t fail when the program is wrong tells you nothing.

Hint

The reading’s car park example used six tests for two boundaries. Your answers to Exercise L3.6 are four of them.

4. Putting it together

Exercise L3.8 (LO-3.2, ★★★, about 25 minutes)

A year is a leap year if it divides by 4, except that a year that divides by 100 is not, unless it also divides by 400. So 2024 is a leap year, 2023 isn’t, 1900 isn’t (it divides by 100 but not by 400), and 2000 is.

Write a script, scratch/leap.py, that asks for a year with the prompt Year: and prints either 2024 is a leap year or 2023 is not a leap year. The check types 2024, 2023, 1900, 2000, 2100, and 2400. You may use if, elif, and else, or one condition built with and and or.

Hint 1

“Divides by 4” is year % 4 == 0.

Hint 2

Check the narrowest rule first: divides by 400, then divides by 100, then divides by 4.

Summary

In this lab you:

  • predicted comparisons, Boolean expressions, and which values count as true;

  • wrote scripts that choose with if, elif, and else;

  • fixed a chain whose order made branches unreachable, and the or mistake;

  • named a decision’s boundaries, and wrote tests that catch mistakes at each one; and

  • turned a rule with exceptions, the leap year, into a decision.

Next, the Module 3 practice gives you more exercises with answers, and Module 4 makes programs repeat themselves with loops.