创建一个面临名称错误的预订程序

creating a reservation program facing a Name error

我正在编写一个程序来创建预订,但我一直收到 NameError。我相信它超出了范围,但我不完全确定如何解决它。尝试将其分配给全局,但也没有用。我想知道是否有人对此有解决方案?谢谢。

    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( )

错误信息:

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

这是代码的固定版本(添加了一些漂亮的打印逻辑)。你不需要 globals,你只需要事情以正确的顺序发生,并且每个函数都按照它说的做。

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()

主要问题是 reserve_seat 中的代码确实应该在调用 reserve_seat 之前发生——即确定要保留哪个座位的部分!我将该代码移至 main() 并修复它,以便 reserve_seat 保留座位 - 它不打印任何内容,它不提示用户对于输入,它只是尝试保留您要求的座位以及 returns 是否成功。

main 函数现在处理确定 rowseat 是什么的任务(通过提示用户——你已经编写了这部分代码,但它在错误的地方,你实际上并没有使用你提示的值)以便它可以使用它们来调用 reserve_seat。它还处理打印结果的步骤。

我还将 get_seating_chart 更改为 make_seating_chart 因为它是一个创建图表的函数(而不是获取现有图表)——如果您多次调用它并且它会覆盖全局变量结果,它将重新制作图表(删除您所做的任何更改),所以最好弄清楚其中的区别!请注意,更新后的 main() 函数恰好调用 make_seating_chart() 一次,然后从那时起重复使用相同的 seating_chart