Geekflare is supported by our audience. We may earn affiliate commissions from buying links on this site.
Share on:

How to Create a Tip and Split Calculator in Python

Tip and Split Calculator in Python
Invicti Web Application Security Scanner – the only solution that delivers automatic verification of vulnerabilities with Proof-Based Scanning™.

Let’s learn how to create a Tip and Split calculator in Python.

It is a great personal project to build to practice your Python skills. Furthermore, this tutorial will teach you how to create the application in two ways, first as a command-line tool and second as a GUI tool.

Preview

We are going to build the application in two ways. First, we will build a simple Python shell script that prompts the user for input and writes the output.

Screenshot-from-2023-01-05-19-30-39

Second, we will give the program a Graphical User Interface using Tkinter.

Screenshot-from-2023-01-05-15-11-29

Program Specification

The program receives three inputs:

  • The bill amount
  • The tipping percentage
  • The number of people sharing the bill

Using these inputs, the program will compute the following output:

  • Each person’s contribution to the bill
  • Each person’s contribution to the tip
  • Each person’s total contribution

Algorithm

To achieve this, the Tip and Split calculator will follow a very simple algorithm outlined below:

  1. Receive the inputs: bill_amount, tip_percentage, number_of_people
  2. Calculate the tip amount by multiplying the bill_amount * tip_percentage / 100
  3. Divide the bill_amount by number_of_people to get each person’s contribution to the bill.
  4. Divide the tip_amount by number_of_people to get each person’s contribution to the tip.
  5. Lastly, add the contributions to the bill and the tip to get the total amount payable.

Prerequisites

To follow this tutorial, you should know and understand Python programming language. For this tutorial, knowledge of basic concepts, including functions and classes, is required.

In addition, you should have Python installed in your system. If it is not, head over to the Python website and download it. Alternatively, Geekflare has an online Python compiler where you can run your Python code in the browser with no environment setup at all.

Building the Calculator with a Command Line Interface

Create a Project Folder

To begin, navigate to an empty folder in your system. In my case, I am using Ubuntu 22.04, so to create a folder and navigate to it using the terminal; I need to enter the following command:

mkdir tip-calculator && cd tip-calculator

Create the Python File

Next, create the script file where we will write the Python script. In my case, I will use the touch command to do so:

touch main.py

Open the Script File with Your Favourite Code Editor

To begin writing the code to the script, open the file with your favourite code editor. I am going to be using nano which is a terminal-based text editor.

nano main.py

Receive the Input

With this done, we can add the following lines of code to the top of the file:

# Receiving input for bill amount as a floating point number
bill_amount = float(input("Bill amount: ")) 

# Receiving input for the tip percentage as a floating point number
tip_percentage = float(input("Tip percentage: "))

# Receiving the input for the number of people as an integer
number_of_people = int(input("Number of people: "))

Basically, this receives the input and casts the data type of each input from a string to the most appropriate type.

Calculate the Tip Amount

Next, we calculate the tip amount by multiplying the tip percentage by the bill amount.

tip_amount = bill_amount * tip_percentage / 100

Divide the Bill and Tip to Get Each Person’s Contribution to the Two

# Calculating each person's bill contribution
bill_contribution = bill_amount / number_of_people

# Calculating each person's tip contribution
tip_contribution = tip_amount / number_of_people

Calculate the Total Contribution

Next, add the individual contributions to determine the total contribution per person.

total_contribution = bill_contribution + tip_contribution

Display the Results

Lastly, output the results to the user.

# Displayinnng the results
print("Bill contribution per person: ", bill_contribution)
print("Tip contribution per person: ", tip_contribution)
print("Total contribution per person: ", total_contribution)

Testing the Tip and Split Calculator

Finally, your script file should look like this:

# Receiving input for bill amount as a floating point number
bill_amount = float(input("Bill amount: ")) 

# Receiving input for the tip percentage as a floating point number
tip_percentage = float(input("Tip percentage: "))

# Receiving the input for the number of people as an integer
number_of_people = int(input("Number of people: "))

tip_amount = bill_amount * tip_percentage / 100

# Calculating each person's bill contribution
bill_contribution = bill_amount / number_of_people

# Calculating each person's tip contribution
tip_contribution = tip_amount / number_of_people

total_contribution = bill_contribution + tip_contribution

# Displaying the results
print("Bill contribution per person: ", bill_contribution)
print("Tip contribution per person: ", tip_contribution)
print("Total contribution per person: ", total_contribution)

At this point, feel free to test-run your application using the following command:

python3 main.py
Screenshot-from-2023-01-05-19-32-16

Building the Tip and Split Calculator with GUI

In the next portion of this tutorial, we will implement the same application but with a Graphical User Interface. To build the GUI, we will use a package called Tkinter.

Setting Up

Tkinter is a package built into Python’s Standard Library. This means it was installed by default when you installed Python.

However, on Linux machines with Python installed by default, TKinter is not preinstalled to save space. Therefore, you need to install it manually using the following command:

sudo apt-get install python3-tk

Create a Project File

To begin, create a file where the Python script will be stored. After you create the file, open it with your preferred text editor.

touch gui.py

Import Tkinter

Next, import the Tkinter package by adding the following line to the top of the file.

import tkinter from tk

Create the User Interface

Then, we can begin creating the user interface.

# Creating the window
window = tk.Tk()

# Creating the Window title
tk.Label(text="Tip and Split Calculator").pack()

# Create an input field
tk.Label(text="Enter bill amount").pack()
ent_bill = tk.Entry(width=40)
ent_bill.pack()

# Create and entry for the tip percentage
tk.Label(text="Enter tip percentage").pack()
ent_tip = tk.Entry(width=40)
ent_tip.pack()

# Create an entry for the number of people
tk.Label(text="Enter the number of people").pack()
ent_people = tk.Entry(width=40)
ent_people.pack()

# Create the Enter button
btn_enter = tk.Button(text="Enter")

The above code created a window containing all the User Interface widgets. In addition, it created a label that will serve as the window’s title.

Next, it created labels and entry fields for the three inputs: bill_amount, tip_percentage and number_of_people. Lastly, it created a button that the user will click to run the calculation.

Create a Function to Calculate the Outputs

After this, we can create a function to handle the click of the Enter button. This function will take the values of the entry fields and use them to calculate the outputs using the algorithm mentioned before. Lastly, it will create a label to display the output and update the window.

def handle_click(event):
    # Collecting the inputs from the entry fields using the get method
    # Also type casting the inputs from the default string data type
    bill_amount = float(ent_bill.get())
    tip_percentage = float(ent_tip.get())
    number_of_people = int(ent_people.get())
    
    # Calcuating the amount to be paid as a tip
    tip_amount = bill_amount * tip_percentage / 100
    
    # Calculating the bill contribution of each person at the table
    bill_contribution = bill_amount / number_of_people 

    # Calculating the tip contribution of each person at the table
    tip_contribution = tip_amount / number_of_people

    # Calculating the total contribution of each person
    total_contribution = bill_contribution + tip_contribution

    # Creating the output string
    output = f'Bill per person: {bill_contribution} \n Tip per person: {tip_contribution} \n Total per person: {total_contribution}'
    
    # Creating a label for the output text
    tk.Label(text=output).pack()

    # Updating the window to reflect the UI changes
    window.update()

The code in the above function has been explained through comments explaining each major step.

Attaching the Event Handler to the Button

Next, we bind the event handler to the button click event. The button click event in Tkinter is represented by the string ‘<Button-1>‘. To bind the event to the event handler, we use the bind method of the button. Add this line of code beneath the function definition:

btn_enter.bind('<Button-1>', handle_click)
btn_enter.pack()

Lastly, to keep the window running, we call the mainloop method of the window object.

window.mainloop()

And we are done!

Testing the Tip and Split Calculator

You can run the application using the following command:

python3 gui.py

This should open up the window as follows:

Screenshot-from-2023-01-05-20-27-26

You can run the calculator with sample input:

Screenshot-from-2023-01-05-20-29-09

Final Words

In this tutorial, we created a tip and split calculator in two ways. The first uses a terminal-based CLI tool. The second is a GUI tool using Python’s Tkinter. This tutorial shows how to build a simple Python project. If you need to brush up or polish your Python skills, here is a Datacamp course.

Next, you can check out how to create a random password generator in Python.

Thanks to our Sponsors
More great readings on Development
Power Your Business
Some of the tools and services to help your business grow.
  • Invicti uses the Proof-Based Scanning™ to automatically verify the identified vulnerabilities and generate actionable results within just hours.
    Try Invicti
  • Web scraping, residential proxy, proxy manager, web unlocker, search engine crawler, and all you need to collect web data.
    Try Brightdata
  • Semrush is an all-in-one digital marketing solution with more than 50 tools in SEO, social media, and content marketing.
    Try Semrush
  • Intruder is an online vulnerability scanner that finds cyber security weaknesses in your infrastructure, to avoid costly data breaches.
    Try Intruder