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 Branch and Loop

UnboundLocalError: local variable 'tickets_remaining' referenced before assignment

How can I solve this? UnboundLocalError: local variable 'tickets_remaining' referenced before assignment

tickets_remaining = 100  

#run this program until tickets finished.

def runProgram():
    #Output how many tickets are remaining using the tickets_remaining variable
    print(("There are {} tickets remaining, be sure to grab it quick!!!").format(tickets_remaining))

    #Gather the users' name and assign it to a new variable.
    user_name = input("What is your name?   ")

    #Prompt the users by name and ask the user how many tickets they want
    ticket_number = int(input("Welcome to the concert, {}. How many tickets do you want to purchase?   ".format(user_name)))

    if ticket_number < tickets_remaining:
        #Calculate the price (number of tickets x ticket price) and assign thhat to a variable
        total_price = TICKET_PRICE * ticket_number

        #output the price to the screen
        print('You have ordered {} tickets. The total price is ${}'.format(ticket_number,total_price))

        #prompt users if they want to proceed? Y/N
        user_proceed = input('Do you want to proceed to the payment process? Y or N   ')
        user_proceed = user_proceed.upper()

        #If they want to proceed, 
        if user_proceed == "Y":
            #we output it on the screen "sold" to confirm purchase. 
            #Gather credit card information and process it.

            #and then decrement the ticket remaining.
            tickets_remaining -= ticket_number

        #otherwise just thank them
            print('Thank you, {}'.format(user_name))
        print('We are sorry, there are only {} tickets available.')

if tickets_remaining > 0:
    print("We have sold out all the tickets")

1 Answer

Whats up Jun...

On the error, tickets_remaining is being called as a "local variable". That gives us a hint that you're having some scope issue with it.

tickets_remaining is a global variable and in Python, there is no problem in using it inside a function. However, you're not just using it, you're updating it from inside the function. To change the value of a global variable from inside a function in Python, according to the w3schools.com you must reference it inside the function using the global keyword.

This will do the work:

def runProgram():
    global tickets_remaining
    #rest of the block

reference: https://www.w3schools.com/python/python_variables_global.asp

thanks, i understand already