2

How to Build a Tip Calculator in Python

 2 years ago
source link: https://hackernoon.com/how-to-build-a-tip-calculator-in-python
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How to Build a Tip Calculator in Python

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.
image
Audio Presented by
Speed:
Read by:
Your browser does not support theaudio element.

Teri Eyenike

I am a frontend developer with a particular interest in making things simple and usable.

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

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK