Hard8 marksExtended Response
ProgrammingGeneralpythoncodingloops

AQA GCSE · Question 15 · Programming

A group of people have a meal in a restaurant. Instead of one person paying for the whole meal, each person will pay for what they eat.

Write a Python program that asks each person in the group how much they are paying towards the meal and works out when the bill is fully paid. Each person can pay a different amount.

The program should:

  • get the user to enter the total amount of the bill
  • get a person to enter how much they are paying towards the bill
  • subtract the amount entered from the bill:
    • if the amount left to pay is more than 0, output how much is left to pay and repeat until the amount left to pay is 0 or less
    • if the amount left to pay is 0, then output the message Bill paid
    • if the amount left to pay is less than 0, then output the message Tip is and the difference between the amount left to pay and 0

You should use indentation as appropriate, meaningful variable name(s) and Python syntax in your answer.

How to approach this question

1. **Get Total Bill:** Start by asking the user for the total bill. Use `float()` to allow for pounds and pence. Store this in `total_bill`. 2. **Create a Loop:** A `while` loop is perfect here. The condition should be `while total_bill > 0:`. This means the loop will continue as long as there is money outstanding. 3. **Get Payment:** Inside the loop, ask for the next person's payment amount, again using `float()`. Store it in a variable like `payment`. 4. **Update Total:** Subtract the payment from the total bill: `total_bill = total_bill - payment` or `total_bill -= payment`. 5. **Provide Feedback:** Inside the loop, add an `if` statement: `if total_bill > 0:`. If true, print the remaining amount so the next person knows what is left to pay. 6. **Handle Final Output:** After the `while` loop finishes (which only happens when `total_bill` is 0 or less), you need to determine the final message. 7. Use an `if/else` statement. `if total_bill == 0:`, print "Bill paid". 8. The `else` block will catch the case where `total_bill < 0`. In this case, calculate the tip by making the negative `total_bill` positive (e.g., `tip = -total_bill`). Then print the tip amount.

Full Answer

This problem requires a loop that continues until a certain condition is met (the bill is paid off). This is a perfect use case for a `while` loop. 1. **Initialization:** The program starts by getting the initial state, which is the `total_bill`. Using `float` is important for handling currency. 2. **Looping:** The `while total_bill > 0:` loop forms the core of the program. It will repeatedly ask for payments as long as the bill hasn't been fully paid. 3. **Processing:** Inside the loop, each `payment` is subtracted from `total_bill`. The `if total_bill > 0:` check provides a running update to the users. 4. **Termination and Final State:** The loop terminates when `total_bill` becomes 0 or negative. After the loop, a final `if/else` structure is needed to distinguish between these two outcomes. - If `total_bill == 0`, the bill was paid exactly. - If `total_bill < 0` (the `else` case), it means the group overpaid. The overpayment amount is the tip. Since `total_bill` will be a negative number (e.g., -2.50), we calculate the tip as `-total_bill` to get a positive value (2.50). - Using `:.2f` in the f-string formats the output to two decimal places, which is appropriate for currency.

Common mistakes

✗ Using an `if` statement instead of a `while` loop, which would only allow for one payment.\n✗ Forgetting to handle the case where a tip is paid (i.e., the final balance is negative).\n✗ Incorrectly calculating the tip (e.g., printing the negative number).

Practice the full AQA GCSE Computer Science Paper 1 Python

31 questions · hints · full answers · grading

More questions from this exam