The Round Function and Its Application in Python

Create three input prompts for food, beverage, and tip, then declare a constant variable for New York State sales tax.

Master Python's rounding function and build a practical Cafe Bill Calculator to handle food, beverage, and tip inputs. Enhance your coding skills by tackling a real-world scenario involving user inputs and tax calculations.

Key Insights

  • Understand Python's built-in round() function, which rounds floats either to the nearest integer or a specified number of decimal places, returning either an integer or float accordingly.
  • Learn to convert user inputs from strings to floats immediately using Python's float() function to handle numeric input effectively, such as converting a score of "89.7" into a floating-point number.
  • Practice building a Cafe Bill Calculator, a coding challenge designed to capture user inputs for food, beverages, and tip percentage, along with applying a fixed tax rate (e.g., New York State sales tax of 8.875%) to demonstrate practical use of input handling and data conversion.

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.

Now rounding, there's the round function that takes a float and a number of decimal places, or it could just take a float. If you pass round a float, it rounds off number, or returns the rounded off number. And if you pass in a second argument, the number of decimal places returns rounded off int, returns float to specified number of decimal places.

If you say you want, you have some big, long decimal, and you could say, I want it to two decimal places. So you'd feed in the number, and then the number of decimal places. All right, let's take that for a spin.

We'll say, let's say we have x equals 8.7654. And we can print x and the type of x. And that'll, of course, be a float. Let's run everything. Now we have inputs.

So whenever you have inputs, and you run all, it's going to make you go back and do all the inputs. OK, so we have a float, 8.7654. Now let's round it to two decimal places. Let's round it to nearest int, actually.

We'll say x equals round x. If you print x and the type of x now, you should get an int of 8, or 9, rather. It's going to round it up. Yep.

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.

Next up, a stringy float. Let's say we're going to say, let's call it game score equals. We're going to do another input.

We're going to floatify an input, right? We're going to have an input, but we're going to immediately take whatever's typed in here and turn it into a float. And then we will print the game score and the type of the game score. There's the float.

That's a desk, 89.7. That's a float as well. If you type in an integer, it'll just put a 0 there. And that is good for that.

76, it'll put the 0, because you're saying you want to float. All right. Challenge.

This one's kind of tricky, but here's what we're going to do. We're going to make a challenge out of it, and then I'll, of course, show you the solution. But I invite you to tackle it on your own.

Don't feel at all bad if it's too much. It's challenging. Put it that way.

So what you can do is make three inputs, just like we made an input here. And you're going to capture the inputs for food, bed, and tip. So the user is going to be prompted to type in a food, beverage, and tip, food amount, beverage amount, and a tip percentage.

We're calling this little challenge Cafe Bill Calculator. And then we're going to declare a variable for tax, New York State sales tax, which will equal 8.875%, which you can write as 8.875, or you can do the decimal conversion and just make it 0.08875. If you do type it as 8.875, you would have to divide by 100, which is probably the way to go. Because when you're asking someone for a tip, they're not going to put 0.18, right? They're going to put 18.

So you should be ready to handle the conversion of tip, right? You don't multiply by 18. You multiply by 0.18 to calculate a tip. So the convention, remember, is on a sales tax, something that's not changing.

You would make that an integer. And then what we're going to do is just print all the variables to start. Just print them out.

Just get the inputs working and print it out. So that's challenge. We can call that part A, right? We're not doing any math with the results or anything.

We're just seeing if we can get the inputs. So pause. Make your three inputs.

You don't need an input for the sales tax because the user doesn't get to type that. That's a constant. So declare your constant for the New York City sales tax, which you basically, here, we can do this one together.

It's already done there. You want to do the three inputs. Make three inputs to capture food, bev, and tip entered by user, who would be like the waiter or waitress, your server or the bartender or whomever.

You've seen it, right? They've got the little color-coded boxes they're clicking on. It's the same logic. Pause and come back when you've got your three inputs working or you've decided you want to see the solution.

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