Search code examples
pythonnameerror

creating a reservation program facing a Name error


I was making a program to create a reservation and I keep getting a NameError. I believe it is out of scope but I am not completely sure how to fix it. Tried assigning it to global but it didn't work either. I was wondering if anyone had a solution to this? Thank you.

    def get_seating_chart( ):
    global seating_chart
    seating_chart =   [['O', 'X', 'X', 'O', 'O'],
                                    ['O', 'O', 'O', 'O', 'O'],
                                    ['O', 'O', 'O', 'X', 'O'],
                                    ['X', 'O', 'O', 'O', 'O'],
                                    ['O', 'O', 'O', 'O', 'X'],
                                    ['O', 'X', 'O', 'O', 'O'],
                                    ['O', 'O', 'O', 'O', 'O'],
                                    ['O', 'O', 'O', 'O', 'O'],
                                    ['O', 'O', 'X', 'O', 'O'],
                                    ['O', 'O', 'O', 'O', 'X']]
    return seating_chart

def reserve_seat(row, seat, seating_chart):
    row = 0
    seat = 0
    if seating_chart[row][seat] == 'X':
        print("Sorry, Seat taken!")
        return False
    else:
        seating_chart[row][seat] = "X"
        print(seating_chart)
        return True
    if (choice == 2):
            print("Make a Reservation")
            print("--------------------")
            #file = open("reservation.txt", "r")
            #first = input("Enter Passenger First Name:")
            #last = input("Enter Passenger Last Name:")
            print(("Printing the Seating Chart..."))
            print(get_seating_chart( ))
            int(input("Which row would you like to sit in?"))
            int(input("Which seat would you like to sit in?"))
            print(reserve_seat(row, seat, seating_chart)(seating_chart))
            if success:
                print("YaY! seat reserved!" )
            #generate confirmation code
            #write reservation (name, row, seat, code) to reservations.txt file
            else:
                print("Sorry, try again.")
    def main( ):
    seating_chart = get_seating_chart( )
    success = reserve_seat(row-1, seat-1, seating_chart)
    
main( )

error message:

Traceback (most recent call last): File "/Users/tylerolznoi/Desktop/Python Projects/FinalProject/final_project_files/res_system_[tjo5fh].py", line 86, in print(reserve_seat(row, seat, seating_chart)(seating_chart)) NameError: name 'row' is not defined


Solution

  • Here's a fixed version of the code (with some pretty-printing logic added). You don't need globals, you just need things to happen in the right order, and for each function to do the thing it says it does.

    def make_seating_chart():
        return[
            ['O', 'X', 'X', 'O', 'O'],
            ['O', 'O', 'O', 'O', 'O'],
            ['O', 'O', 'O', 'X', 'O'],
            ['X', 'O', 'O', 'O', 'O'],
            ['O', 'O', 'O', 'O', 'X'],
            ['O', 'X', 'O', 'O', 'O'],
            ['O', 'O', 'O', 'O', 'O'],
            ['O', 'O', 'O', 'O', 'O'],
            ['O', 'O', 'X', 'O', 'O'],
            ['O', 'O', 'O', 'O', 'X']
        ]
    
    def print_seating_chart(seating_chart):
        print("\n".join(" ".join(row) for row in seating_chart))
    
    def reserve_seat(row, seat, seating_chart):
        if seating_chart[row][seat] == 'X':
            return False
        else:
            seating_chart[row][seat] = "X"
            return True
    
    def main():
        seating_chart = make_seating_chart()
    
        print("Make a Reservation")
        print("--------------------")
        # file = open("reservation.txt", "r")
        # first = input("Enter Passenger First Name:")
        # last = input("Enter Passenger Last Name:")
        print("Printing the Seating Chart...")
        print_seating_chart(seating_chart)
        row = int(input("Which row would you like to sit in?"))
        seat = int(input("Which seat would you like to sit in?"))
    
        success = reserve_seat(row, seat, seating_chart)
    
        if success:
            print("YaY! seat reserved!")
            print_seating_chart(seating_chart)
            # generate confirmation code
            # write reservation (name, row, seat, code) to reservations.txt file
        else:
            print("Sorry, Seat taken!")
    
    main()
    

    The main problem was that you had code inside reserve_seat that really should happen before reserve_seat is called -- namely, the part that figures out what seat to reserve! I moved that code into main() and fixed it so that reserve_seat only reserves the seat -- it doesn't print anything, it doesn't prompt the user for input, it just tries to reserve the seat that you ask for and returns whether it was successful.

    The main function now handles the task of figuring out what row and seat are (by prompting the user -- you'd written this part of the code but it was in the wrong place and you weren't actually using the values that you prompted for) so that it can use them to call reserve_seat. It also handles the step of printing what the result was.

    I also changed get_seating_chart to make_seating_chart since it's a function that creates the chart (rather than getting an existing one) -- if you call it more than once and it overwrites a global variable with its result, it'll re-make the chart (erasing any changes you've made), so it's good to be clear on the difference! Note that the updated main() function calls make_seating_chart() exactly once and then reuses the same seating_chart from that point forward.