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 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
seal-mask
STAFF
.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 :)

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!