Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
A function ticket_price(age) gives the price of a museum ticket: under 5 is free (0), under 13 pays 50, under 60 pays 100, and 60 and more pay 70. Write a test table with at least eight cases, and a loop that runs it.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Plan. Work out the expected prices from the rules. The edges are at 5, 13 and 60. Test both sides of each edge, an ordinary age in each group, and the smallest age.
| Age | Expected | Why |
|---|---|---|
| 0 | 0 | the smallest age |
| 4 | 0 | just below the first edge |
| 5 | 50 | at the edge |
| 12 | 50 | just below the second edge |
| 13 | 100 | at the edge |
| 59 | 100 | just below the third edge |
| 60 | 70 | at the edge |
| 90 | 70 | a very old age |
The function and the loop.
Check. All eight lines show PASS. If you change age < 13 to age <= 13, the test at 13 fails and finds the fault.
Answer: The table above and the loop.
Log in to join the discussion.
Loading…