How to Build a Tip Calculator in Pythonby@terieyenike
4,148 reads

How to Build a Tip Calculator in Python

tldt arrow
Read on Terminal Reader

Too Long; Didn't Read

In this article, you 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. Building this tip calculator will be done in Python. Other use cases where this application can be useful are ride-sharing apps like Uber or those working for delivery companies. The use case can be adapted to many other uses and can be expanded beyond to include more details. The tip calculator quickly gives you a quick estimate of the total payment.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - How to Build a Tip Calculator in Python
Teri Eyenike HackerNoon profile picture

@terieyenike

Teri Eyenike

I am a software developer focused on creating content through...

Learn More
LEARN MORE ABOUT @TERIEYENIKE'S EXPERTISE AND PLACE ON THE INTERNET.
react to story with heart

Have you had a problem splitting payments in a restaurant after a nice meal with friends? Other use cases where this application can be useful are 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.

Getting Started

Before writing a line of code, let’s write a pseudo code for the process of building the app:

  • The tip calculator will have a message welcoming you to use the app
  • Allow user to enter the amount of the bill
  • Give users the option to choose the amount of tip
  • An input to enter the number of people to split the bill amongst
  • Next, calculate the bill to show how much each person pays
  • Finally, format and round the amount to pay to two decimal places

Creating a Tip Calculator in Python

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 the user 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 is to 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

image

Wrapping Up

The use case of this application can be adapted to many other uses aside from a tip calculator and can be expanded beyond to include more details. The possibilities are endless.

In this article, you have learned how to build a tip calculator and how fast this can help when you don’t want to calculate and split bills with paper and a pen manually.

Try this method to save yourself a lot of stress on your next hangout.

Learn More

Teri Eyenike HackerNoon profile picture
by Teri Eyenike @terieyenike.I am a software developer focused on creating content through technical writing and documentation.
Portfolio

RELATED STORIES

L O A D I N G
. . . comments & more!
Hackernoon hq - po box 2206, edwards, colorado 81632, usa