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

Urs Angst
Urs Angst
6,006 Points

Python parameter mismatch in the calculate function

Hello there,

Below my code. What I seem not to understand is how the calculate_price function with the parameter number_of_tickets right at the beginning of the program picks up the number of tickets in the parameter ticket_amount_request further down. I wonder why their variable names don't have to match.

Thanks a lot, Urs

TICKET_PRICE = 10

SERVICE_CHARGE = 2

tickets_remaining = 100  


def calculate_price(number_of_tickets):
        return (number_of_tickets * TICKET_PRICE) + SERVICE_CHARGE

while tickets_remaining >=1:
    print("There are {} tickets remaining.".format(tickets_remaining))
    personal_name = input("What is your name?  ")
    ticket_amount_request = input("How many tickets would you like to order, {}?  ".format(personal_name))
    try:
        ticket_amount_request = int(ticket_amount_request)
        if ticket_amount_request > tickets_remaining:
            raise ValueError("There are only {} tickets left.".format(tickets_remaining))
    except ValueError as err:
        print("Oh no, we ran into an issue: {}. Please try again.".format(err))
    else:
        total_amount = calculate_price(ticket_amount_request)
        print("Your total amount is {}.".format(total_amount))
        proceed_decision = input("Would you like to continue to checkout? (Y/N)  ")
        if proceed_decision.lower() == "y":
            #TODO: Gather credit card information and process it.
            print("SOLD!")
            tickets_remaining -= ticket_amount_request
        else:
            print("Thank you for visiting us today, {}!".format(personal_name))
print("The show is sold out.")

[MOD: added ```python formatting -cf]

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 67,636 Points

Hey Urs Angst, good question!

When calling a function, Python passes arguments “by object reference”. All variable names are really just a label for the object reference.

In the line,

total_amount = calculate_price(ticket_amount_request)

there are three labels:

  • calculate_price points to the function
  • ticket_amount_request points to the value to be passed into the function
  • total_amount points to the result of the call

In the function parameter list of the function calculate_price, the label number_of_tickets points to whatever object is passed into it.

So number_of_tickets gets pointed to the same object that ticket_amount_request is pointing at.

This decoupling of label names allows the function to be written without concern of the names of the object label passed into the function.

To see this literally, add a print(id(number_of_tickets)) inside the function and a print(id(ticket_amount_request)) near the call to the function. The same id value (location in memory) means the two labels are pointing at the same object.

Post back if you need more help. Good luck!!!

Urs Angst
Urs Angst
6,006 Points

Hi Chris Freeman ! Thanks a lot for your answer. It helped and clarified! Best, Urs