How to Make a Tip Calculator in Python

Make a Tip Calculator in Python

Let’s Learn together:

This blog helps to write Python code that will calculate tips for a restaurant bill and share the money among friends, Also we will discuss the various concepts used to write this code

Below are concepts, That we need to be aware of to write this code. If you want to skip to the tip calculator code directly check here

  • Data type
  • Type Conversion
  • Mathematical Operations
  • Number Manipulation

Data Type & Type Conversion

Data types are just a classification of data, So based on that we can decide, which operator can be used to manipulate the data.

Some the example of datatype

  • String
  • Integer
  • Float
  • Boolean

Let’s try a quick example, To understand why datatype is needed

If you want to get a length of a string, we can use len() function (To know more about len() check here)

Print (len(“python”)) => 5

The above code will give you the number of chars in the word “python”

Let’s pass a numeric value to the len() function and see what happens,

Print (len(123))

TypeError: object of type 'int' has no len() on line 1 

Yes, This will break the code and will show the “TypeError”, Where it is expecting a string but got an integer value

To resolve the issue, we can just convert the integer to a string as below and this is called “Type Conversion

print(len(str(123))) or print(len(“123”))

=> 3

Similarly, we can do this for float, integer, string

float (variable)

int(variable)

str(variable)

Python Mathematical Operations

Below are the types of mathematical operations used in python

  • 2 + 2 – Addition
  • 2 – 2 – Subtraction
  • 2 * 2 – Multiplication
  • 2 / 2 – Division
  • 2 ** 2 – Exponential => 2 to the power of 2

If you have more than one operator in a code, There is a certain level of priority

PEMDAS – Priority of the operation from Left -> Right

  • P – Parentheses
  • E – Exponents
  • M – Multiplication
  • D – Division
  • A – Addition
  • S – Subtraction

Example

Let’s see, How the below code works with the above rules

print(3 * (3+3) /3-3)

  1. (3+3) = 6
  2. 3*6 = 18
  3. 18/3 = 6
  4. 6-3 = 3

3 is the Final Answer

Number Manipulation

It’s basically manipulating the numbers based on our needs. Will see a quick example

print (8/3) => will give a float number => 2.66666667

But, We need just 2 digits after the decimal point. For that, we can use the round function

Print (round(8/3,2)) => 2.67

Now, Let’s see, How to create a tip calculator by using the above concepts

Code Below

print("Welcome to the tip calculator\n")

bill_amt=input("What was the total bill? ")

tip_amt=input("What percentage tip you like to give? 10, 12 0r 15? ")

split=input("How many people to split the bill? ")

per_person=round((float(bill_amt)/int(split))*float("1."+tip_amt),2)

final_amt="{:.2f}".format(per_person)

print("Each person should pay:"+ final_amt)

Output:

Welcome to the tip calculator
What was the total bill?  150
What percentage tip you like to give? 10, 12 0r 15?  12
How many people to split the bill?  5
Each person should pay:33.60

There are multiple ways to achieve the above program and I have shown you one of the ways, Please try the above code for yourself

Check here to check the code in the Python editor

We can have a quick exercise of creating the same program with a different logic and share it with us 🙂

Start a forum conversation or chat with us if you have any questions on the examples

Similar Posts