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

Hanneke Lu
Hanneke Lu
560 Points

why use the new variable 'total_due' instead of the variable 'total'?

Why does Craig introduce the variable total_due when asking for input from the user? (For the number of people he is keeping the variable number_of_people, not introducing a new variable)

I didn't introduce the variable total_due and just used the variable total. The program works. Is there something I am overlooking as to why it's good to introduce the variable total_due, as Craig does?

Many thanks!

import math

def split_check(total, number_of_people):
    return math.ceil(total / number_of_people)

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

print("Each person needs to pay ${}".format(amount_due))
Hanneke Lu
Hanneke Lu
560 Points
import math

def split_check(total, number_of_people): return math.ceil(total / number_of_people)

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

print("Each person needs to pay ${}".format(amount_due))```
Dave StSomeWhere
Dave StSomeWhere
19,822 Points

I think you are overlooking the concept that there is no relationship between the variables inside and outside the function definition. So, both your total and Craig's total_due are new variables (outside of the function).

8 Answers

Gregor Vollmaier
Gregor Vollmaier
10,986 Points

It does not matter whether you name the new variable total or total_due. In each case, it is a new variable, since you are creating it outside of the function. "total" in "def split_check(total..." is valid only inside the function.

You can choose to name the new variable the same as the function parameter (total), but you don't have to, as Craig didn't.

Hanneke Lu
Hanneke Lu
560 Points

Thank you Dave and Gregor for your help!

Rana Olk
Rana Olk
235 Points

Thanks for your help everyone... Great question Hanneke Lu > I came here to ask the very same thing. Totally stumped me.

So...I still don't get it. I get that variables inside and outside a function don't necessarily have any relationship.

However... In this particular case, when we say "total" we mean, the total amount of the check. When we say "total_due", we still mean, the total amount of the check.

So why did Craig bother defining the input we need (the total amount of the check) as something different than the variable we are using in the function? Is there a reason for this, perhaps, that we will find out in later modules?

It bothers me when everything doesn't make perfect sense:)

Thanks in advance.

Dave StSomeWhere
Dave StSomeWhere
19,822 Points

Functionally, it just doesn't matter what you call a variable (as long as you follow the naming rules).

Choosing names that are meaningful is more of an etiquette, useful member of the community type thing. So, Craig could of called his variable silly_variable_1 and everything would work, but the next person to look at the code wouldn't know at a quick glance what is happening.

Might as well call a sum of numbers a "total" and if you are representing the total amount due - "total_due".

Often various programming/it shops have very specific naming conventions for variables/files/servers and so on...

I didn’t understand this either.

The only thing that makes sense to me is that in his original example he input the numbers himself: *amount_due = split_check(84.97, 4)

The second example the numbers are obtained via input questions: *amount_due = split_check(total_due, number_of_people)

Its the same because instead of inputting 84.97 in the script you now have input(“What is the total? “) that is getting the information from the user.

The original example in the video was a way where you could write the script and obtain the information just by adding the numbers and hitting check_please.py and the script would return the answer.

The second example in the video now prompts you with two questions when you hit check_please.py. So the information total_due substitutes the 84.97 from the original and does not affect the outcome.

Correct if I'm wrong, but I believe after reading the responses that 'total' inside the function is simply a placeholder. It will be subbed out for total_due a few lines down.

Even if u change total to total_due the output does not change i guess it does not really matter even if u change the name also i have the same question in my mind when i saw the video. if changing the name does change something then it will be somewhat usefull. if anybody hav proper answer to this question plz reply.

I was wondering the same thing. After reading Gregor Vollmaier 's explanation, I understand now. It's just confusing to use total_due when total already exists.

Wojciech Ratajczak
Wojciech Ratajczak
1,925 Points

Check the thread "Question about variable names in functions". Confusing is acctualy the name "number_of_people", not the total. total_due = total, number_of_people = number_of_people in this example of function. If you have a look this way, everything will be clear.