Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics Functions and Looping Returning Values

Reuben Haley
seal-mask
.a{fill-rule:evenodd;}techdegree
Reuben Haley
Python Development Techdegree Student 233 Points

Rounding Up Values

from the video

import math

def split_check(total,number_of_people):
     cost_per_person = math.ceil(total / number_of_people)
     return cost_per_person
amount_due = (split_check(555,5))

print("Each person owes ${}".format(amount_due))

Is it acceptable to do this round up the amount_due instead of the cost_per_person or the vice versa?

import math

def split_check(total,number_of_people):
     cost_per_person = total / number_of_people
     return cost_per_person

amount_due = math.ceil(split_check(555,5))

print("Each person owes ${}".format(amount_due))

1 Answer

Jason Larson
Jason Larson
7,539 Points

Considering that you're rounding up to the nearest integer, I don't think there would be an issue. There's a margin of error of up to 99 cents this way, so I can't imagine that you'd run into a split that would cause an issue. Even if you weren't rounding by this much, the difference would be minute, so I wouldn't worry about it. Someone who is very good with math may take exception, or if you were doing something that was concerned with the specificity out several decimal points, but for this type of real-world application, either way is fine.

Stylistically, I would recommend that you do the rounding within the function to keep the code cleaner so that all calculations related to splitting the check are handled in one place, and if you decide to change something, everything is together when you're looking at the code. You're not having to jump back and forth between the function and the section where the function is called. I know in this tiny example, that doesn't seem important, but when you're dealing with hundreds or thousands of lines of code, it can make a big difference, especially when troubleshooting.