Creating a Café Bill Calculator with Python

Demonstrates calculating a cafe bill using Python by taking user inputs, computing subtotal, tax, tip, and total with proper currency formatting.

Create an efficient Python-based cafe bill calculator that accurately computes subtotals, tips, and taxes, then neatly formats your final bill output. Discover how to implement proper currency formatting to enhance your financial calculations and presentation.

Key Insights

  • Use Python's input() nested within float() to effectively handle decimal inputs for food, beverage, and tip percentages, allowing accurate calculation of the bill.
  • Apply specialized currency formatting in Python using "{:, .2f}".format() to ensure monetary amounts consistently display trailing zeros, commas for numbers over 1,000, and correct currency presentation.
  • Calculate the bill components separately—subtotal (sum of food and beverage), tip (percentage of subtotal), and tax (percentage of subtotal)—before formatting and displaying them in a clear, itemized bill format.

Note: These materials offer prospective students a preview of how our classes are structured. Students enrolled in this course will receive access to the full set of materials, including video lectures, project-based assignments, and instructor feedback.

This is a lesson preview only. For the full lesson, purchase the course here.

Okay, we're back to the solution for Challenge Part A: the Cafe Bill Calculator. We're going to say food_total—we could call it—is going to equal float. We have to allow for decimal input for food.

So the user will be prompted to enter food. We're going to say dollars so they know it's a nested input inside a float. So you've got to end it with two closing parentheses.

And then we'll do the beverage, and we'll do the—that’ll be the bev_total. And then the tip—we're not going to call it the tip_total. We'll call it the tip_percent, right? You have to calculate the total.

The user's not going to—you're not going to write in the total. You're going to just put in your percent tip and let it calculate. So you'd put a 20 in here.

Example: 18,20—like that. And then let's print everything. Print all the variables, right? food_total—we'll just do it in one line—bev_total, tip_percent, SALES_TAX.

Python for Data Science Bootcamp: Live & Hands-on, In NYC or Online, Learn From Experts, Free Retake, Small Class Sizes,  1-on-1 Bonus Training. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

Let's just print them all out one after another. By convention, constant names that never change are all uppercase, right? Okay. Run.

So food—we'll say 87.50. Bev—we'll say 62. Tip percent—we'll go 20.

And there you go. There are the values. Okay.

So that's Part A. In Part B, we want to do the math. So let's slide on down to Part B. And when we get the numbers to come out—remember, we're doing money—we don't want to have any odd decimal formatting.

If we do math and we get a long decimal, we want to round it to two decimal places. And if we get commas in the money, we would like to have a comma—like if the bill's over $1,000, have a comma. We can use this syntax—this little weird squiggly quote thing with `.format()`.

And you would pass in your bill amount, right? Like X or whatever the value is. That would be an alternative to rounding X to two decimal places because it would make sure you get a trailing zero if it's like 0.50. Because notice—we typed 87.50 and then we printed it, and it doesn't say 87.50, it says 87.5. Likewise, with the beverage, it should say 62.00. When you just round or, right, or you just make floats without rounding, it's not going to supply a trailing zero. But if you do this format, it will. We're going to use this little format move, which is very hard to remember.

You just have to kind of know it exists, and then go look it up and copy it when you need it. So that'd be like Python Googling: format currency. Let's see how that would work.

Format currency—that's basically it. There it is right here.

Boom. Formatted amount = dollar sign. Boom.

You're not going to remember, right? Squiggle colon comma dot 2F closing curly, closing quote, `.format()` passing the amount, which is basically what we're doing—except we didn’t, you know, we don't have the dollar sign. If we have the dollar sign, that would be called currency format. You could do round, but it's kind of better not to, really.

That's a concatenation—better than round, which doesn't give you the dollar sign. It gives you a number.

Then you have to CONCATENATE the dollar sign. Now it's got to be a string. This handles all the stringification.

It handles making sure you have two decimal places, right? As opposed to like 0.5, you get 0.50, and it gives you the comma even if you go over a thousand. So it's good currency formatting—a little move—and you set that equal. So you would say, you know, currency = number.

In fact, let's try one of these right now. Let's say X. That should be 8.50. currency_X. And let's print currency_X. That should be $8.50. Yup. It worked.

So that's good. We have that in our back pocket. So once we do our—this is a stringification move, right? That is a string.

So you wouldn't do this until you're done with your math, right? You can't do math with strings. But once you're done with your math, you come in—you know, you want to present it, output it so it looks nice and, you know, looks like money, right? You're done with the math, so you don't care if it's not a number anymore.

And then you stringify it. Make sure it's got, again, the closing zero. And we could also say—if it's a really long number—it'll also give you the comma, which is great. So to ensure that you always get two decimal places for currency—dollars and cents—use this special formatting. But be aware: this stringifies the value.

So you must do all your math first. All right. Now—Challenge—carry on.

You've got your inputs. Calculate the cafe bill from user input and print the itemized bill. Use the currency formatting.

You can redo these guys. I would keep everything in one cell. Why don’t you copy this: food_total, bev_total, tip_percent.

Just copy that. You’ve got your SALES_TAX. So you’ve already got, you know, Python cafe guest check.

Okay. There’s the beginning of it. There are your three inputs.

And here is your constant. And now we print all the variables. We're, you know, basically recreating some priority.

Oh, it doesn’t. Okay. So the total—what we're going to do is now we have to cook up the total.

We printed all these variables, but we don’t have the subtotal, the tip_total, the tax_total. You know, the tip—we should also print. We have the tip_percent.

We should print the tax_percent. We have the tip_percent, the subtotal. Let’s move the tip_percent down.

Let me move this. Okay. So what we’re going to do is—let’s do the subtotal right after the food and bev.

Then we’ll calculate the tip. Then we’ll do the tax. Then we’ll add everything together.

That’s your "please pay"—this is your total, your grand total. Pause and finish the job.

Challenge Part B. What you need to do is—you’ve got your inputs: food, bev, tip_percent. You need to add the food and the bev together to get the subtotal. Then you need to calculate the tip as a percentage of the subtotal.

Add that together. Then you need to calculate the sales tax as a percentage of the subtotal. Don’t tax the tip.

Okay. So calculate the tax before you do the tip, and keep them separate. In other words, just don’t add it all together until you’re done.

Add subtotal plus tip_total plus tax_total. So wait until you get all three of those. Then add them all together.

That way you’re not tipping on the tax or taxing the tip. Just keep them separate. Okay. So pause and work on that.

Okay. We're back. So let's knock it out of the park here.

We've got our subtotal. We'll declare a variable for that. We'll say subtotal = food_total + bev_total.

And we can log that. The idea is—we’re printing this out as we go. It’s going to look like a bill, right? Coming out just like so.

Just all these print statements add up to resemble a guest check. Okay. So that's our subtotal.

Now the tip_percent is—we’re capturing the inputted tip. The tip_total, though, is going to equal the subtotal, right, times the tip_percent divided by 100—because the tip_percent is going to be entered as 18 or 20, not 0.18. We'll log—print the tip_total then. 78.76,22.

Our tip is 33.88. All right. Notice we're not getting the double zero on the subtotal because we have not done the formatting on that. Although we can—we could actually take care of that as we go.

Let’s just copy this. And when we output tip_total, let’s turn the tip_total into a formatted tip_total. And let’s turn the subtotal into a formatted subtotal.

And print. And we're having a big old party here with all that money we're spending. Tip_total.

Oh, you don’t need to do the dollar sign on the tip_total because it's built into the formatting. Ditto with the subtotal. I got the percent on the wrong side.

That's okay though. All right. There's your New York sales tax.

So the tax_total is going to equal the subtotal times NYC_SALES_TAX divided by 100. And we want to express that formatted—so we don't need to feed in the dollar sign. That’s supposed to be your tax_total.

This will be the tax_percent. Right. We're calculating the sales tax.

We have the sales tax percent. We’re calculating the tax_total by multiplying the subtotal by the sales tax rate and then dividing by 100—because 8.875 is, of course, 0.08875. And then we're printing that out and formatting the total tax so it looks nice. And then we’re finally going to say total—or grand_total, if you like—equals subtotal + tip_total + tax_total.

And the grand_total is that. Whoa. Yikes.

That’s a pretty big bill. But there it is. So that's the solution to Challenge Part B.

Brian McClain

Brian is an experienced instructor, curriculum developer, and professional web developer, who in recent years has served as Director for a coding bootcamp in New York. Brian joined Noble Desktop in 2022 and is a lead instructor for HTML & CSS, JavaScript, and Python for Data Science. He also developed Noble's cutting-edge Python for AI course. Prior to that, he taught Python Data Science and Machine Learning as an Adjunct Professor of Computer Science at Westchester County College.

More articles by Brian McClain

How to Learn Python

Master Python with hands-on training. Python is a popular object-oriented programming language used for data science, machine learning, and web development. 

Yelp Facebook LinkedIn YouTube Twitter Instagram