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 tote, we could call it, is going to equal float. We have to allow for decimal input 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 got to end it with two closing parens.

And then we'll do the beverage and we'll do the, that'll be the Bev tote. And then the tip, we're not going to call it the tip tote. 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 tote, we'll just do it in one line. Bev tote, 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 will say 87,50. Bev will say 62. Tip percent will 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 decimals.

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 dot 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 handle, 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, right? It's not going to say 87.50. 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 equals dollar sign. Boom.

You're not going to remember, right? Squiggle colon comma dot 2F closing curly closing quote dot 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, it's, 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, little move, and you set that equal. So you would say, you know, currency equals, you know, 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, right, 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 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 and foo total, bev total, tip percent.

Just copy that. You got your sales tax. So you've already got, you know, Python cafe guest check.

Okay. There's the beginning of it. There's your three inputs.

And here is your constant. And now we print all the variables. We're, you know, we 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 these, 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 way. 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, 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 till you get all three of those. Then add them all together.

That way you're not tipping on the tax or taxing the tip and 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 got our subtotal. We'll declare a variable for that. We'll say subtot equals food tot plus bev tot.

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, you know, 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 in to 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 tote.

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 tote or grand tote, if you like, equals subtote plus tip tote plus tax tote.

And the grand tote is that. Whoa. Yikes.

That's a pretty big bill. But there it is. So that's the solution to the 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