Last active
May 9, 2025 17:10
-
-
Save McSnurtle/5d9320834c17ace267470398842e3a3f to your computer and use it in GitHub Desktop.
This is a demo-only version of the function `utils.calculator.total_per_person()` modified for readability and explanation. The original code can be found in the receipt-calculator repo by Mc_Snurtle at https://github.com/McSnurtle/receipt-calculator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # This is a demo-only version of the function `utils.calculator.total_per_person()` | |
| # modified for readability and explanation | |
| def total_per_person_demo(receipt: Receipt) -> Dict[str, float]: | |
| """Calculates the total amount of money owed per person on the receipt based on what they | |
| purchased. | |
| Args: | |
| receipt (Receipt): The receipt object containing the user / purchase data to calculate | |
| Returns: | |
| Dict[str, float]: A dictionary of each user and how much they owe as the values. | |
| """ | |
| debts: Dict[str, float] = {} # An empty tracker of debts that will look something like: | |
| # { | |
| # "Jerry": 15.47, | |
| # "George": 19.55, | |
| # "Elaine": 59.99, | |
| # "Kraimer": 4566.99 | |
| # } | |
| for person in receipt.people: | |
| debts[person] = 0.0 # Set the base value for all users of items on the receipt | |
| for item in receipt.items: | |
| # Add the cost of the item + it's tax and tip percentages to the running total of the | |
| # end user of the item | |
| debts[item.user] += item.cost * (1 + item.tax + item.tip) | |
| return debts |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment