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

Ingrid Matthews
Ingrid Matthews
1,465 Points

Why isn't "total" defined?

I was puzzled by this. In my workspace I did exactly what Craig did, leaving "total" undefined, and it worked, but then I changed "total" to "total_due" in those two places, and it ALSO worked. At 9:20 in the video, you can see that he's using "total_due" below, but at the top in the definition of split_check it's just "total." ??. Thanks for any clarification!

2 Answers

Ingrid Matthews
Ingrid Matthews
1,465 Points

Thank you, I really appreciate it!

Steven Parker
Steven Parker
229,670 Points

Ingrid Matthews — Normally, you'd select "best answer" on the answer that contained the information that helped solve the issue. :see_no_evil:

Ingrid,

I had the same question. Basically, the parameters inside the function are not related to the variables outside it. Here is my source code that proves your instinct:

import math

def split_check(x, y):
    return math.ceil(x / y)

total_due = float(input("What is the total?  "))
number_of_people = int(input("How many people?  "))
amount_due = split_check(total_due, number_of_people)


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

The function parameters can be named whichever way you want, so as long as you use the same name inside the function to refer to the argument that was passed it. Eg.

def print_a_number(number):
    print(number)

print_a_number("foobar")

Please note that the above is a bad example, it's just to illustrate the point. Even though the function parameter is called number... I've passed in a string as an argument instead. With that said, you always want to be descriptive, and make your code read well.

Ingrid Matthews
Ingrid Matthews
1,465 Points

Thank you Pedro! But I'm still confused... my question wasn't about strings vs. numbers, it was about the specificity of the name given to the function. Would the computer read "total" and "total_due" to mean the same thing, without being told to?

Hi Ingrid, sorry if I was not clear. The function parameter can be named "total" but it could also be named anything else. That variable only exists (scope) inside the function itself. The function is being called with "total_due" as an argument, which is then reassigned to the local variable "total" inside the function. Both variables names are not related, you can named them the same or not. I recommend you write/paste the code on this website http://www.pythontutor.com/visualize.html as it will show you how the code works under the hood.