Day 2 - Tip Calculator💵🤑

Day 2 - Tip Calculator💵🤑

If you are the type of person who always enjoys friends' night/day out and has to tip and split the bill amongst yourself, then stick with me; you will find this very helpful. In this article, I will be giving a detailed step-by-step approach to coding your very own tip calculator.

This article is part of a series, and if you have yet to check out the previous article - Day 1, you probably should because we covered some fundamental topics in Python that would be instrumental in this project. If you are reading this further, I am assuming you have already, and you are good to go. In that case, let's do this.

Just as the title implies, the goal of this project is to create a tip calculator that accepts the total bill, tip percentage, and number of people splitting the bill as input and then returns the amount each person is to pay. Pretty simple, right? To achieve this, we will cover the following Python basics - Data types, Type checking, Type casting, Arithmetic operators, Number manipulation, and F-strings.

Data types, Type checking, and Type casting

In Python, data types are used to define the type of a variable, and for this project, we will be looking at just three of them - integer, float, and string. You should be familiar with the string data type already, but just as a reminder, they are text data types usually denoted by either single quotation marks '' or double quotation marks "" . Integer and float are numeric data types, and below is the syntax.

age = 20  # I am an integer (int)
pi = 3.14  # I am a float

int - holds signed integers of non-limited length.

float - holds floating decimal points and it's accurate up to 15 decimal places.

When declaring a variable in Python, we do not specify the data type, so it is essential to know how to check the type of your variable, especially when you are unsure, and we can do this using the type() function.

type("Hello, world!")  # returns <class 'str'>, i.e., type string
type(123)  # returns <class 'int'>, i.e., type integer
type(2.8)  # returns <class 'float'>, i.e., type float
type(age)  # returns <class 'int'> because we previously declared age as 20.

What if we want to change our variable from one type to another? This is called type casting in Python, and we can do that just as quickly using the syntax below.

age = "20"  # a string
new_age = int(age)  # changes the string to an integer.

num1 = 2  # an integer
num2 = float(num1)  # becomes 2.0, i.e, the type has been changed to a float.

Let us now see how this will help us create our tip calculator.

# accept the total bill as input
total_bill = float(input("What is the total bill? "))
# accept the tip percentage as input
tip_percent = int(input("How many percent tip are you offering - 10, 12, or 15? "))
# finally, accept the number of people splitting the bill as input
num_of_people = int(input("How many people are splitting the bill? "))

You can probably already see Python's typecasting in action because the input() function returns user input as a string, which makes it necessary to change the type so it is suitable for us to work with.

Arithmetic & Assignment Operators

Operators in Python are used to perform operations on variables and values, and there are different types of operators, including arithmetic operators, assignment operators, logical operators, etc. Our focus is on the assignment and arithmetic operators.

Arithmetic operators are used with numeric values to perform everyday mathematical operations. They include: -, +, *, /, %, **, and //.

addition = 2 + 2  # 4
substraction = 2 - 2  # 0
multiplication = 2 * 2  # 4
division = 2 / 2  # 1
modulus = 2 % 2  # 0 - divides the number and return the remainder.
exponentiation = 2 ** 3  # 8
floor_division = 15 // 2  # 7 - rounds the result down to the nearest whole number.

Assignment operators are used to assign values to variables, and although we haven't explicitly stated it, we have encountered an assignment operator - =. Others include: -=, +=, *=, /=, %=, **=, //=, etc. We would only discuss the listed operators in this article.

a = 2  # assigns 2 to a
a += 2  # 4, same as a = a + 2, adds 2 to a and a becomes 4.
a -= 2  # 0, same as a = a - 2, subtracts 2 from a and a becomes 0.
a *= 2  # 4, same as a = a * 2, multiply 2 and a and a becomes 4.
a /= 2  # 1, same as a = a / 2, divides a by 2 and a becomes 1.
a %= 2  # 0, same as a = a % 2, divides a by 2 and a becomes the remainder 0.
a **= 3  # 8, same as a = a ** 3, raise a to the power of 3 and a becomes 8.
a //= 2  # 1, same as a = a // 2, divides a by 2 and a becomes 1 instead of 1.0.

With this new knowledge, we should be able to calculate the tip amount and the amount each person will pay. Let's implement that in our tip calculator.

# calculate the tip amount
tip_amount = tip_percent / 100  # divide the tip percent by 100
tip_amount *= total_bill  # multiply the tip amount with the total bill
total_bill += tip_amount  # add the tip amount to the total bill.

# calculate the amount each person will pay
# divide the total bill by the number of people paying
individual_bill = total_bill / num_of_people

Now that we have calculated the amount each person will pay, all that is left is to display the amount. However, we still need to cover one more topic to help us show the correct amount.

Number manipulation & F-strings

When carrying out divisions in Python, the results are always returned in floating decimal points, which can be up to 15 decimal points, so it is necessary to know how to round them to the desired number. One way of doing this is with the round() function.

x = 10 / 3  # returns 3.33333...
rounded_x = round(x, 2)  # returns 3.33
num = round(x)  # returns 3

round() - returns a float number that will be rounded to the decimal places which are given as input. If the decimal places to be rounded are not specified, it is considered as 0, and it will round to the nearest integer.

Another way of manipulating floating point numbers is with the F-string. Apart from this, it provides a concise and convenient way to embed Python expressions inside string literals for formatting.

x = 10 / 3
print(f"10 / 3 = {x:.2f}") # this displays 10 / 3 = 3.33

To create an f-string, prefix the string with the letter “ f ”.

:.2f formats the number to 2 decimal places.

I believe we now have all we need to complete our tip calculator. We had to learn number manipulation first because we didn't want to display the amount each person would pay as a floating point number with more than two decimal places. It's not like you have ever seen a bill of $50.345 😅.

print(f"Each person is paying: ${individual_bill:.2f}")

We have created a tip calculator using data types, type casting, assignment and arithmetic operators, and f-string. Let's combine all the codes.

# accept the total bill as input
total_bill = float(input("What is the total bill? "))
# accept the tip percentage as input
tip_percent = int(input("How many percent tip are you offering - 10, 12, or 15? "))
# finally, accept the number of people splitting the bill as input
num_of_people = int(input("How many people are splitting the bill? "))

# calculate the tip amount
tip_amount = tip_percent / 100  # divide the tip percent by 100
tip_amount *= total_bill  # multiply the tip amount with the total bill
total_bill += tip_amount  # add the tip amount to the total bill.

# calculate the amount each person will pay
# divide the total bill by the number of people paying
individual_bill = total_bill / num_of_people
print(f"Each person is paying: ${individual_bill:.2f}")

The output should look like:

tip calculator output

Yay!!! Our tip calculator is ready for use. Next time, you don't have to stress about using your phone calculator when you go out with your friends or partner😉; you can use your tip calculator. What a lifesaver!

Conclusion

You have come a long way, and that's wonderful. The basic information you learned will benefit your Python coding journey, so practice it often. Feel free to customize your tip calculator whichever way you want or add additional features. Test the tip calculator below or view the complete code.

See you next time!

References

  1. Python operators

  2. F-strings in Python

  3. Python data types

  4. round() function in Python

Did you find this article valuable?

Support Glowcodes by becoming a sponsor. Any amount is appreciated!