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 trial

Python Python Basics Functions and Looping Returning Values

Andrew Wyant
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Andrew Wyant
UX Design Techdegree Graduate 11,851 Points

How is the value cost_per_person being stored in amount_due?

at 4:20 Craig says this is the case but what part of the code is doing that? Wouldnt we need a line similar to amount_due = cost per person? Thanks

1 Answer

Asher Orr
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Asher Orr
Python Development Techdegree Graduate 9,409 Points

Hi Andrew!

The function split_check takes two arguments:

def split_check(total, number_of_people)

Those arguments (total, number_of_people) are used in the variable cost_per_person.

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

Then the function returns the operation performed by cost_per_person.

Check out the example below:

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

split_check(84.97, 4)

Here, I'm calling the split_check function. It will return 21.25, but that information isn't being stored anywhere.

If I store it in a variable, I can refer to that information later. Like this:

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

amount_due = split_check(84.97, 4)
You asked: wouldn't we need a line similar to amount_due = cost per person?

Craig said "our function returned the value cost_per_person, which we stored in amount due." Here's another way to think about this:

"The split_check function calculates cost_per_person, and it returns that information. The amount_due variable stores whatever is returned by split_check.

It doesn't store the cost_per_person variable itself, because that variable is used inside the split_check function."

I hope this helps. Let me know if you have questions!