A coffee shop gives one stamp per drink on a card that holds 20 drinks. Every 4th stamp earns a biscuit, every 5th a pastry, and the 20th a cake with no biscuit or pastry. Figure 2 is a program that should print Free biscuit for a biscuit stamp, Free pastry for a pastry stamp, Free cake for the 20th, and Nothing free for any other drink; it has three gaps to fill. Each day the shop also adds up what it gave away: biscuit £1, pastry £2.50, cake £3.
Write the Python that fills gaps 1, 2 and 3 in Figure 2 so every stamp gets the correct reward.
Our own questions on this topic, marked straight away, so you see what to revise. Free now.
Show model answer for 02.1Hide model answer for 02.1
Gap 1: i % 5 == 0
Gap 2: print("Free biscuit")
Gap 3: print("Nothing free")The cake test comes first, so stamp 20 never reaches the pastry or biscuit tests.
How the marks are earned
- Gap 1 is the condition i % 5 == 0.
- Gap 2 is print("Free biscuit").
- Gap 3 is print("Nothing free").
- Capital or small letters make no difference to the marking.
- Small spelling slips in the messages are ignored.
- Missing brackets on print cost one mark only, once, and are not penalised again for the other gap.
Why it scores
Gap 1 is the multiple-of-5 test, which sits between the cake test and the multiple-of-4 test. The next branch handles multiples of 4, so gap 2 prints the biscuit message. The else branch catches every other stamp, so gap 3 prints the nothing message.
Testing the most specific case first is what stops stamp 20 being given a pastry as well.
Revise Selection (IF Statements)Write a program that reads how many free biscuits, pastries and cakes were given away into separate meaningful variables, then prints their total value.
See how you would score on Variables & ConstantsShow model answer for 02.2Hide model answer for 02.2
free_biscuits = int(input("Free biscuits given: "))
free_pastries = int(input("Free pastries given: "))
free_cakes = int(input("Free cakes given: "))
day_value = free_biscuits * 1 + free_pastries * 2.5 + free_cakes * 3
print("Total value in pounds:", day_value)How the marks are earned
- At least one of the three counts is read from the keyboard into a variable.
- User input is stored in three separate variables, one each for biscuits, pastries and cakes.
- An expression that works out the total value correctly: 1 per biscuit, 2.5 per pastry and 3 per cake.
- The total value is output.
- The case of the code is ignored; messages with the input and output statements are optional.
- Gaps and spaces are ignored unless they change the logic.
- The same syntax error repeated in the three input statements costs only one mark.
- If the code has any error, the maximum is 3 marks.
Why it scores
Each count is read with input() and made a whole number with int(), in its own variable with a meaningful name. The total multiplies each count by its price (1, 2.5 and 3) and adds the results. The last line prints that total once.
Revise Variables & Constants