Cell In[2], line 2 if age < 12 ^ SyntaxError: expected ':'
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.
ticket_price(8) is requested; Python rejects the code before it can run.
70; execution stops at the comparison inside ticket_price.
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'
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?
- Add the missing colon in Program 1; change
age: inttoage: strin Program 2; changeage <= 12toage < 12in Program 3. - Add the missing colon in Program 1; pass
int(age_typed)toticket_pricein Program 2; changeage <= 12toage < 12in Program 3. - Add the missing colon in Program 1; pass
int(age_typed)toticket_pricein Program 2; changeage >= 65toage > 65in Program 3. - Add the missing colon in Program 1; pass
int(age_typed)toticket_pricein Program 2; leave Program 3 unchanged because it runs without an error.