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 All Together Now Cleaner Code Through Refactoring

cole kubossek
cole kubossek
2,740 Points

calculate_price variable....

In the example we defined the function calculate_price(number_of_tickets). But in the cost calculation below we use cost = calculate_price(tickets). Shouldn't the variable in here be (number_of_tickets).

I got a little lost on how those end up being used properly though they are 2 different variables. If that makes sense.

2 Answers

Megan Amendola
.a{fill-rule:evenodd;}techdegree seal-36
Megan Amendola
Treehouse Teacher

When you define a function, the parameters it takes can be named anything you want. Then when you use them inside of the function, you need to use the same name. For instance:

def multiply(num1, num2):
    return num1 * num2

Here I made a function that multiplies two numbers together. The parameters are passed into the function and then multiplied together. Since I named them num1 and num2, I will need to use those names inside the function in order to access the values being passed in.

Now when I call the function, the arguments being passed into the function will most likely be named something else because you may be passing in variables.

tree = 5
bug = 3

multiply(tree, bug)
multiply(6, 7)

The arguments that I am passing into the function have names that are different than the function declaration because I am passing in variables. Here tree holds the number 5 and bug holds the number 3. You can also pass in values themselves into a function like my second function call shows.

Hopefully, this helps :)

Alex Groat
Alex Groat
444 Points

Ok, so you are saying that in "cost = calculate_price(tickets)" the input for tickets will be used for both tickets and number_of_tickets because of the parameter set in the cost variable?

cole kubossek
cole kubossek
2,740 Points

Thanks for the quick and helpful response!