Skip to Content
Course content

31: Practice Exercise: Building a Tip Calculator

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Before we dive into building your own calculator, I want to show you a snippet of code. I wrote this a few years ago when I was first getting comfortable with Python, and it does exactly what you'd expect a tip calculator to do—except it doesn't actually work. It crashes immediately.

bill = input("What was the total bill? ")
tip_percent = input("What percentage tip would you like to give? ")

tip_amount = bill * (tip_percent / 100)
print("The tip amount is: " + tip_amount)

If you try to run this, Python will throw a TypeError. It’s a frustrating moment when you feel like the logic is sound, but the language is fighting you.

The String Math Conflict

The problem here is a classic. In Python, the input() function always returns a string, regardless of whether the user types a number or a word. When I try to divide tip_percent by 100, I'm essentially asking Python to divide a piece of text by a number. Python doesn't know how to do that, so it gives up and crashes.

Even if the division worked, the print statement would fail too. You can't use the + operator to combine a string ("The tip amount is: ") with a number (the calculated tip) without converting the number back into text first.

Casting and Formatting for Precision

To fix this, we need to "cast" our inputs. Casting is just a fancy way of telling Python, "I know this looks like a string, but I want you to treat it as a float (a decimal number)."

I also want to address something that happens once you fix the crash: the "ugly decimal" problem. If your total comes out to something like 15.666666666666668, it looks unprofessional. We can handle this using f-strings with a formatting modifier to lock the output to two decimal places.

bill = float(input("What was the total bill? "))
tip_percent = float(input("What percentage tip would you like to give? "))

tip_amount = bill * (tip_percent / 100)
total_bill = bill + tip_amount

# The :.2f tells Python to format this float to 2 decimal places
print(f"The tip amount is ${tip_amount:.2f}, making the total ${total_bill:.2f}")

By wrapping the input() calls in float(), the math now works perfectly. And by using :.2f inside an f-string, we ensure the output looks like actual currency. It's a small detail, but in software engineering, the difference between a tool that feels "broken" and one that feels "polished" is usually in these small formatting choices.




📋 Practical Task

The Multi-Person Split Tip Calculator

Now it's your turn. I want you to build a fully functional Tip Calculator that doesn't just calculate the tip, but also splits the final bill between a group of people.

Your program should do the following:

  • Ask the user for the total bill amount.
  • Ask the user for the tip percentage they'd like to leave (e.g., 12, 15, or 20).
  • Ask how many people are splitting the bill.
  • Calculate the total bill (original bill + tip).
  • Calculate how much each person owes.
  • Print the final amount per person, formatted to two decimal places.

Challenge: Make sure your program can handle a bill of $0.00 without crashing!

Rating
0 0

There are no comments for now.

to be the first to leave a comment.