These questions follow Making Decisions. A decision in code is a question with exactly one answer acted on — and most of the trouble comes from the values sitting right on the boundary between two branches.

Reading

  1. Predict the output for mark = 70, then for mark = 69, then for mark = 80.
    if mark >= 80:
        print("Excellent")
    elif mark >= 70:
        print("Good")
    elif mark >= 50:
        print("Pass")
    else:
        print("Not yet")
  2. What does this print when temperature is exactly 30?
    temperature = 30
    if temperature > 30:
        print("Open the windows.")
    print("done")
  3. Find the fault and say which of the three kinds of error it is.
    days_late = 3
    if days_late > 0:
    print("Overdue")
  4. Trace it. What prints when minutes is 0?
    if minutes > 0:
        if minutes < 30:
            print("Short session")
        else:
            print("Full session")
    else:
        print("Day off")

Writing

  1. Write the three-tier version of an overdue message: 0 or fewer days is on time, up to 7 days gets a reminder, anything more gets a letter home. Test it with 0, 7, and 8.
  2. A club meets only if at least five members are present and the room is booked. Write it with nested if statements, and print a different message for each of the three outcomes.
  3. Somebody writes if mark = 80: and Python refuses to run the file at all. Why, and what did they mean?
  4. Challenge. For the code in question 1, which four values of mark would you test to be confident every branch works, and why those four?

Answers