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 trialHanneke Lu
560 Pointswhy 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))
Dave StSomeWhere
19,870 PointsI 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
10,986 PointsIt 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
560 PointsThank you Dave and Gregor for your help!
Rana Olk
235 PointsThanks 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
19,870 PointsFunctionally, 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...
Andres Perez
9,130 PointsI 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.
Alec Hawley
13,346 PointsCorrect 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.
pushpinder singh
Python Web Development Techdegree Student 1,755 PointsEven 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.
Mounlier Rodriguez
276 PointsI 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
1,925 PointsCheck 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.
Hanneke Lu
560 PointsHanneke Lu
560 Points