Blog Post #20: Part 1 Recap & Project: Build a Simple Tip Calculator

Congratulations on reaching the end of Part 1 of our Python series! This is a major milestone. You have journeyed from understanding what Python is, to writing your first interactive scripts, and you’ve picked up an entire toolkit of fundamental concepts along the way.

In this post, we will first briefly recap the core concepts we’ve covered. Then, we’ll put all that knowledge to work by building a complete, practical program from scratch: a simple tip calculator.

Part 1 Recap: Your Foundational Toolkit

Over the last 19 posts, you have acquired a solid foundation in Python programming. Here’s a quick look back at what’s now in your toolkit:

  • Core Concepts: We started by learning what Python is (Post #1) and its basic grammar, like how indentation defines structure (Post #6). We also learned to leave ourselves notes using comments (Post #7).
  • Data and Variables: We discovered how to store data in variables with descriptive names (Post #8) and learned about the fundamental data types: numbers like int and float (Post #9), text with str (Post #10), and the logic building blocks of bool (Post #11).
  • Making Things Happen: We learned to perform math with a whole suite of basic (Post #12) and advanced (Post #13) arithmetic operators, and how to modify variables concisely with assignment operators (Post #19).
  • Interacting with the User: We made our programs interactive using the input() function (Post #15) and learned the critical skill of type casting with int(), float(), and str() (Post #16) to manage the data we receive.
  • Creating Output: We saw how to join strings with concatenation (Post #17) and mastered the modern, powerful f-string for creating clean, readable output (Post #18).

Project: The Tip Calculator

It’s time to combine these skills. We will build a command-line program that calculates the correct tip for a restaurant bill and then splits the total amount evenly among a number of people.

Create a new file called tip_calculator.py and follow along.

Step 1: Greeting the User

A good program is a friendly program. Let’s start with a simple welcome message so the user knows what the program does.

# Greet the user
print("Welcome to the Tip Calculator!")

Step 2: Getting the Inputs

Next, we need to ask the user for three pieces of information: the total bill, the percentage they want to tip, and the number of people splitting the bill. We’ll use the input() function for each and immediately cast the string result to the correct data type (float or int).

# Get the necessary information from the user
bill = float(input("What was the total bill? $"))
tip_percentage = int(input("What percentage tip would you like to give? 10, 12, or 15? "))
people = int(input("How many people to split the bill? "))

We use float() for the bill because it’s likely to have a decimal, and int() for the tip percentage and number of people, as those should be whole numbers.

Step 3: Performing the Calculations

Now we have our inputs, we can perform the math. We’ll calculate the total bill (including the tip) and then determine how much each person needs to pay.

# Calculate the total bill with the tip included
tip_as_multiplier = 1 + (tip_percentage / 100)
total_bill = bill * tip_as_multiplier

# Calculate how much each person should pay
bill_per_person = total_bill / people

Step 4: Displaying the Result

Finally, we need to present the answer to the user in a clear, formatted way. Since we are dealing with money, the result should be rounded to two decimal places. We can do this brilliantly right inside an f-string.

# Display the final amount each person should pay, formatted to 2 decimal places
print(f"Each person should pay: ${bill_per_person:.2f}")

The :.2f inside the curly braces is a format specifier. It’s a mini-instruction that tells the f-string to format our number as a float with exactly 2 decimal places.

The Complete Code

Here is the full program in one block. Make sure you understand how each part contributes to the final result.

# Greet the user
print("Welcome to the Tip Calculator!")

# Get the necessary information from the user
bill = float(input("What was the total bill? $"))
tip_percentage = int(input("What percentage tip would you like to give? 10, 12, or 15? "))
people = int(input("How many people to split the bill? "))

# Calculate the total bill with the tip included
tip_as_multiplier = 1 + (tip_percentage / 100)
total_bill = bill * tip_as_multiplier

# Calculate how much each person should pay
bill_per_person = total_bill / people

# Display the final amount each person should pay, formatted to 2 decimal places
print(f"Each person should pay: ${bill_per_person:.2f}")

Congratulations and What’s Next for Part 2

You have successfully built a complete, interactive program that solves a real-world problem. You’ve taken all the foundational concepts from Part 1 and synthesized them into a working application. Well done!

In Part 1, our programs have been very linear—they run from top to bottom and do the same thing every time. In Part 2: Logic and Control Flow, we will learn how to make our programs much smarter. We’ll start by learning how to make decisions with if statements, allowing our code to follow different paths based on different conditions.

Author

Debjeet Bhowmik

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com

Leave a Comment