Lesson 2.4 · Write it
Ticket price
A whole programPython or Java7 tests, 3 of them hidden
On a phone? Put the lines in order instead
Write a program that reads an age (a whole number, 0 or more) and prints the age and the ticket price on one line.
- Under 12: price is 5 (child).
- 65 or over: price is 7 (senior).
- Everyone else: price is 10 (adult).
- The format is
Age: N Price: Pon one line, where N is the age and P is the price. - Spaces at the end are ignored.
Your program is run several times. Only what it prints is checked.
In Python, use int to convert. In Java, use Integer.parseInt.
Running Java here comes later. You can put this answer's lines in order instead. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.
Examples
Your program is run 7 times: the 4 examples below, and 3 hidden ones that check the rules above. Only what it prints is checked.
a child under 12 (rule 1)
You type
- typed: 8
It prints
Age: 8 Price: 5
an adult (rule 3)
You type
- typed: 30
It prints
Age: 30 Price: 10
exactly 12: not a child (rule 1 boundary)
You type
- typed: 12
It prints
Age: 12 Price: 10
exactly 65: a senior (rule 2 boundary)
You type
- typed: 65
It prints
Age: 65 Price: 7