Hello World !
Today we are going to learn tip calculator program. We are going to implement it in Python.
Calculating tip when spending good time with our friends seems quite tedious. We are totally reluctant when it comes to calculation, sometimes I end up over paying just to avoid computing(Bad Maths :-P). So, Finally I came up with an idea to a create program for calculating tip and use it whenever needed.
What will be covered:
Data-Types.
Mathematical operators.
F-string
Format()
Lets start with coding:
Start with greeting the user.
print("Welcome to tip calculator.")
Ask the user, what's the total bill?
bill=float(input("What is the total bill? $ "))
NOTE: Taking input as float by typecasting {by default input is in string format for python. Float is a data-type used to store decimal value.
Ask the user what percentage tip would you like to give?
tip=int(input("What percentage tip would you like to give? 5, 10 or 15 ? "))
Ask the user how many people are splitting the bill?
split=int(input("How many people are splitting the bill? "))
Calculation
total_bill= bill+tip/100*bill
original_bill=total_bill/split
print(f"Each person should pay: $ {original_bill}")
NOTE- Calculate total_bill by dividing bill with tip(returns float value) and adding tip on original bill.Finally splitting the bill among friends. F-STRING: it is used to combine string with different datatypes. I consider it as a replacement of string concatenation.
If you like to keep precision in your code/ print the value upto certain decimal places use format function.
final="{.2f}".format(original_bill)
print(f"Each person should pay: $ {original_bill}")
FORMAT Function: it is used to print up-to desired decimal places.
With these steps, we have successfully created a tip calculator program using python. That's it!
I hope you find it helpful.
You can find the copy of program on my github : https://github.com/Samra-qur/100-Days-Coding
Please connect with me on my LinkedIn : https://www.linkedin.com/in/samra-qureshi-a60828194/