Course 3.1Lesson 5 of 5
In this project we write a ticket office. The price depends on the age, as in the ticket ladder, and a weekend adds 20 rupees to every ticket that is not free. We write the rules first, then a test table with the edges, then the program in two steps, and we run the tests. We then extend the office with a rule for groups and a discount code, and test again. The project uses comparisons, if, elif, else, and a second decision that comes after the first.
Try the code and the simulations beside the lesson.
After this lesson you can
A small museum sells tickets. The rules are:
Before the program, work out the expected price for some inputs.
| Age | Weekend? | Expected price |
|---|---|---|
| 4 | no | 0 |
| 4 | yes | 0 (still free) |
| 5 | no | 50 |
| 12 | yes | 70 |
| 13 | no | 100 |
| 59 | yes | 120 |
| 60 | no | 70 |
| 60 | yes | 90 |
The edges are 5, 13 and 60. The rule of the weekend has its own special case: the free ticket. This table is the answer key of the project.
The price by age is the ladder of the last lesson. Then a second decision adds the weekend.
Change age and weekend for each row of the table, and compare the price with your expected one. For the row 4, True: the first step gives 0, and the second step does not add 20, because price > 0 is False. This is the case that a program without the protection gets wrong, and it is why we put it in the table.
The jumps in the graph are the edges: 5, 13 and 60. Those are the ages to put in the test table.
A project is yours when you change it. Choose one, and do it without looking at a solution. After each change, add rows to the test table.
FREE20 gets 20 rupees off.if where you need elif. In step 1, the choices exclude each other.else. The variable price may not exist for an age that is not caught.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
and to write the rule in one line.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
elif in the price ladder, and a separate if for the weekend?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.