Tip Calculator | Python
Today, I created a tip calculator. Functionally, it works by providing the terminal with a few key pieces of information when prompted:
The total of the bill
The percentage to tip
The number of people splitting the bill
In return, this program will provide the total amount that each person is responsible for paying (inclusive of the tip). This was a fairly straightforward program to write and I thoroughly enjoyed creating it. The key take aways from this project were an introduction of how to manipulate data types and use basic math functions. This is certainly a very simple project, but it’s the first I have built in Python, so I am proud. Let’s talk through how it works…
First, the terminal welcomes the user to the tip calculator and requests the total bill amount. Initially the input was directly to the right of the prompt, which I did not like the look of. So, I added a newline escape via the ‘\n’ that you see. Additionally, at this point in my journey, I do not have proper error handling. To prevent the user from feeling tempted to include the dollar symbol in their input and break my logic, I have included it in the prompt.
Next, the program prompts the user to input the percentage of tip they would like to leave. This is stored in the 'tip_percentage' variable. At this point, the number entered is a string, which will provide a Type Error when we start trying to apply math operations to it. However, we can use this to our advantage initially, to simplify our math in the future. The 'tip_converted' variable hold a concatenation of '1.' and the entered percentage from the 'tip_percentage' variable.
For example, if the user enters that they wish to leave a 20% tip, the 'tip_converted' variable will hold it as '1.20'. This enables us to apply a single multiplication operation to find our total bill amount instead of first finding the amount of the tip and then adding it to the original bill amount.
Next, the program asks for how many people will be splitting the bill. This is held in a variable called ‘people’ for later use.
Okay, so here is where the magic happens!
First, the program calculates the total amount inclusive of the tip. This is completed by converting the ‘bill’ variable to a floating point number, which is needed because bills are rarely ever flat dollar amounts. This is then multiplied against the ‘tip_converted’ variable which has also been converted to a floating point number. The result of this operation is stored in ‘bill_with_tip’ variable for future use.
Next, the program divides the total stored in ‘bill_with_tip’ by the number stored in the ‘people’ variable; but not before converting ‘people’ to an integer. This final calculation is stored in the ‘per_person’ variable.
(Hopefully you aren’t dining with what would be considered a fraction of a person, so a float is not needed!)
Finally, the program presents the calculation to the user through the terminal via a print statement. I am using an ‘f-string’ or ‘string literal’ here, which enables me to place the final value (as the variable name) within the string. This is completed by adding ‘f’ at the beginning of the string (before the first quotation mark) and then wrapping the variable name in curly braces. So, what you see here will print: ‘Each person should pay $’
This is followed by the value stored in ‘per_person’. However, notice that ‘per_person’ is encapsulated in the round() function with a ‘2’ as the modifier enabling me to round to the second decimal place. This presents our final result in the expected dollar and cents format.
You can view and download the source code for this project on my GitHub by clicking here. Thank you for your time!