Have you had a problem splitting payments in a restaurant after a nice meal with friends? Another valuable use case for this application is ride-sharing apps like Uber or those working for delivery companies. The tip calculator quickly gives you a quick estimate of the total payment.
In this article, you will learn how to build a tip calculator with an intelligent and thoughtful way to divide payments amongst friends, no matter the number, and give suggestions as to the amount in the tip in percentage.
So, this application will be handy the next time you go out.
Let’s get coding.
Want to try this app? Run and test this Repl using this link.
Before writing a line of code, let’s write a pseudo code for the process of building the app:
A tip calculator is the best application you can have and use when going out as it is accurate with precise calculations. Building this tip calculator will be done in Python.
Create a file called tip_calculator.py
and paste the following code:
# tip_calculator.py
print("Welcome to the tip calculator!")
bill = float(input("What was the total bill? $"))
tip = int(input("How much tip would you like to give? 10, 12, or 15? "))
num_of_split = int(input("How many people to split the bill? "))
calculate_bill = bill * (1 + (tip/100)) / num_of_split
final_amount = "{:.2f}".format(calculate_bill, 2)
if tip == 10:
print(f"Each person should pay: ${final_amount}")
elif tip == 12:
print(f"Each person should pay: ${final_amount}")
else:
print(f"Each person should pay: ${final_amount}")
The following occurs in the code block above:
The bill variable tells the user to enter a float number that accepts decimals using the input prompt
The tip
and num_of_split
variables also allow users to provide the value they desire according to the tip percentage and the number of people.
The calculate_bill
is an operation using PEMDAS to calculate how much each person pays from the declared variable result output
Next, round/format the bill result to two decimal places to ensure it reads like a currency.
The conditional statement is essential to print out the correct message when the condition meets the specified tip using the if, else statement
In this article, you have learned how to build a tip calculator and how fast this can help when you don’t want to manually calculate and split bills with paper and a pen, as Python does the computation.
Try this method to save yourself a lot of stress on your next hangout.