Tip calculator in Python

Ronnie Ford
4 min readMay 29, 2021

Purpose of this lab

To create a basic tip calculator using Python.

What you’ll need to complete this lab

· Text editor (I’m using Vim but use whatever text editor you would like)

· Latest version of Python 3 on your computer

Python features you’ll be using

· Functions (we’ll be using the print and input functions)

· Variables

· Data types

· F-strings

Installing the latest version of Python

You can find the latest version of Python the official website. Use this link (Python3).

Writing the Python script

Optional: Get a piece of paper and a pencil and create an outline for your project. This will give you an idea of what needs to be done in order to accomplish your project, and it will be a great reference point while you’re writing your code.

Please note: I’m using a MAC so your steps may be different if you’re using another operating system.

Create a folder or directory for your code. Type in the below command in order to create your new directory.

You’ll want to make sure you’re in the new directory. To do that type in the below command.

Now that you’re in the new directory you’ll want to create a Python file so you can write your script. To do that type in the below command. You can name this file whatever you like.

I like to comment out the steps needed to complete the task. You can do that by adding a pound sign (#) and then typing out your steps. Python understands that these are just comments and won’t execute this when we run our code.

The first step is to print out “Let’s calculate your tip!”. Type in the below code.

You’ll need to ask the user for the bill amount. You’ll need to use a variable (you can name your variable whatever you would like) to store the bill total. The below line of code will do this for you.

FYI: Periodically run your code. This will help you to see if your code is running the way you want it to, and it will help you with troubleshooting if you get any errors.

If you run your code now it should allow you to enter the bill amount. It should look something like the below screenshot.

You’ll need to ask the user how much they would like the tip amount to be and calculate the total amount with tip. You’ll need to use three variables here. One variable will ask the user for the tip percentage, the second variable will calculate the total bill with tip, and the third variable will round the user’s bill with tip to two decimal places. The below block of code will do this for you.

Your code should look something like the below screenshot.

Finally, we will need to ask the user how many people are splitting the check, and tell them how much each person will owe. We’ll need two variables. One will ask the user how many people are splitting the check and store the input, while the second one will calculate the amount each person will pay. The below code will do that for us.

All we have to do now is just test our code to make sure everything runs correctly, and if it does it will look something like the below screenshot.

You’ve successfully created a tip calculator using Python. I hope you enjoyed the step-by step guide.

--

--