M10) Ticket price errors

A museum sells tickets at 3 prices. Children under 12 pay €5.00, visitors aged 65 or over pay €7.50, and everyone else pays €10.00. Three students each wrote a small program for the rule. Listing 1 shows the 3 programs with the output from running each one directly below its code. In Program 2, the user enters 70 at the prompt.

Listing 1: Three programs for the ticket price (Question 1).
(a) Program 1: ticket_price(8) is requested; Python rejects the code before it can run.
def ticket_price(age: int) -> float:
    if age < 12
        return 5.0
    elif age >= 65:
        return 7.5
    else:
        return 10.0


print(ticket_price(8))
  Cell In[2], line 2
    if age < 12
               ^
SyntaxError: expected ':'
(b) Program 2: the user enters 70; execution stops at the comparison inside ticket_price.
def ticket_price(age: int) -> float:
    if age < 12:
        return 5.0
    elif age >= 65:
        return 7.5
    else:
        return 10.0


age_typed = input("Age: ")
print(ticket_price(age_typed))
Age: 70
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[3], line 11
      7         return 10.0
      8 
      9 
     10 age_typed = input("Age: ")
---> 11 print(ticket_price(age_typed))

Cell In[3], line 2, in ticket_price(age)
      1 def ticket_price(age: int) -> float:
----> 2     if age < 12:
      3         return 5.0
      4     elif age >= 65:
      5         return 7.5

TypeError: '<' not supported between instances of 'str' and 'int'
(c) Program 3: ticket_price(12) runs to completion and prints 5.0.
def ticket_price(age: int) -> float:
    if age <= 12:
        return 5.0
    elif age >= 65:
        return 7.5
    else:
        return 10.0


print(ticket_price(12))
5.0
TipHint: error types
  • A syntax error means Python cannot read the code as valid Python. It is reported before the program starts.
  • A runtime error stops a program while it is running. The last line of its traceback names the error Python found.
  • A logic error means the program runs but gives a result that breaks the stated rule. Python may report no error, so check the output against the rule.

Which set of changes would make all three programs follow the museum’s rule?

  1. Add the missing colon in Program 1; change age: int to age: str in Program 2; change age <= 12 to age < 12 in Program 3.
  2. Add the missing colon in Program 1; pass int(age_typed) to ticket_price in Program 2; change age <= 12 to age < 12 in Program 3.
  3. Add the missing colon in Program 1; pass int(age_typed) to ticket_price in Program 2; change age >= 65 to age > 65 in Program 3.
  4. Add the missing colon in Program 1; pass int(age_typed) to ticket_price in Program 2; leave Program 3 unchanged because it runs without an error.