Course 3.2Lesson 5 of 5
In this project we write the entry rules of a club. Under 18 is never allowed. At 18 or more, a person needs a ticket, but a member enters without a ticket. We write the rules in plain words, build a table of the cases, write the condition with and, or and not, test all the combinations, and then extend the club with a closing time and a guest rule. The project uses combined conditions, a flat decision with elif, and a test table with every branch.
Try the code and the simulations beside the lesson.
After this lesson you can
A club has these entry rules.
Three things can change the answer: the age (under 18 or not), the ticket (yes or no), the membership (yes or no). That gives 2 times 2 times 2, which is eight cases. Write the expected answer for each, from the rules (not from a program).
| Adult? | Ticket? | Member? | Result |
|---|---|---|---|
| no | no | no | denied: too young |
| no | yes | no | denied: too young |
| no | no | yes | denied: too young |
| no | yes | yes | denied: too young |
| yes | no | no | denied: needs a ticket |
| yes | yes | no | welcome |
| yes | no | yes | welcome |
| yes | yes | yes | welcome |
Look at the table. The first four rows are the same. The age decides everything for the young. For adults, we need has_ticket or is_member.
Notice how the order helps. Because the first check removed the young, the second condition does not have to ask about the age again.
Change the three values at the top, one row of the table at a time, and compare the result with the table. You can also use a loop (Chapter 9) to run all eight at once, as you did in the previous chapter.
Three of the eight cases are welcome. The whole bottom row is too young, whatever the ticket and the membership.
Pick one, and do it without looking at a solution. After each change, add rows to your table. With every new yes/no question, the number of cases doubles.
price.and and or without brackets in the middle of a long condition.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:
if with and and or that gives True or False, with no elif. Check it with the table.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
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.