Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialthomas ethridge
1,007 Pointspython variables
I am VERY confused by pythons variables.
def split_check(total, number_of_people): cost_per_person = math.ceil(total / number_of_people) return cost_per_person
amount_due = split_check(84.97, 4)
print("Each person owes ${}".format(amount_due))
so in the first line- split_check equaling the total / number of people makes sense. so why do we create a new variable called cost_per_person which would equal the same exact thing as the split_check variable, which is also the same exact thing as the newest variable amount_due. Why are we not calling of this split_check, instead of renaming it 2 times as cost_per_person and amount_due?
2 Answers
Steven Parker
231,272 PointsThe extra variables are probably used to break the process down into simple steps for easier understanding. But you are correct that the process can be condensed down to avoid the use of the variables, for example this code would perform exactly the same as the original:
def split_check(total, number_of_people):
return math.ceil(total / number_of_people)
print("Each person owes ${}".format(split_check(84.97, 4)))
thomas ethridge
1,007 PointsOK! Thank you. That makes way more sense to me now