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 trialIngrid Matthews
1,465 PointsWhy 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
1,465 PointsThank you, I really appreciate it!
Pedro Cabral
33,586 PointsThe 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
1,465 PointsThank 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?
Pedro Cabral
33,586 PointsHi 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.
Steven Parker
231,186 PointsSteven Parker
231,186 PointsIngrid Matthews — Normally, you'd select "best answer" on the answer that contained the information that helped solve the issue.
Roberto Carrera
219 PointsRoberto Carrera
219 PointsIngrid,
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: