首页 > 解决方案 > UnboundLocalError:分配前引用的局部变量“stdRoom”

问题描述

我一直在尝试使用 try and except 来解决这个问题,但它似乎不起作用,如果我的代码没有可用的房间或者用户还没有输入数字但它只是从头开始返回错误。这是我的代码问题:

        if choose == 2:
            while stdRoom >= 1:
                while dlxRoom >= 1:
                    while prsRoom >= 1:                            
                                
                        try:
                            print("Booking \n"
                                  "Enter room you want to book: \n"
                                  "1. Standard --- Available: ", stdRoom,"\n"
                                  "2. Deluxe ----- Available: ", dlxRoom,"\n"
                                  "3. Presidential Available: ", prsRoom,"\n")
                            booking = int(input("Enter room you want to book: "))
                            if stdRoom < 1:
                                print("Please add a room")
                                main()
                            if booking == 1:
                                stdRoom -= 1
                                print("You have chosen Standard \n")
                                fName = input("Enter first name: ")
                                lName = input("Enter last name: ")
                                days = int(input("Days of stay: "))
                                addPerson = input("Would you like to add another person(P.100)? Y/N ")
                                if addPerson == "Y" and "y":
                                    person = int(input("Enter number of persons: "))
                                    personDisplay = person * 100
                                else:
                                    print("0 persons added")
                                misc = input("Would you like to add extra bed sheets, towel or bed? Y/N ")
                                if misc == "Y" and "y":
                                    print("1. Bed Sheets: 100 \n"
                                          "2. Towel: 50 \n"
                                          "3. Bed: 150 \n")
                                    ask = int(input("Enter misc additions: "))
                                    if ask == 1:
                                        sheets = int(input("Enter number of bed sheets: "))
                                        sheetsAdd = sheets * 100
                                    elif ask == 2:
                                        towel = int(input("Enter number of towel: "))
                                        towelAdd = towel * 100
                                    elif ask == 3:
                                        bed = int(input("Enter number of beds: "))
                                        bedAdd = bed * 100
                                    else:
                                        print("0 items added")
                                else:
                                    print("0 items added")
                                    
                                total = (days * stdPrice + personDisplay + sheetsAdd + towelAdd + bedAdd)
                                print("Room Booking Sucessful: "
                                      "First Name: ", fName,"\n"
                                      "Last Name: ", lName,"\n"
                                      "Days of Stay: ", days,"\n"
                                      "Person's added: ",person,"Price: ",personDisplay,"\n"
                                      "Miscellaneous: \n"
                                      "Bed Sheets: ", sheetsAdd,"\n"
                                      "Towels: ", towelAdd,"\n"
                                      "Beds: ", bedAdd,"\n"
                                      "Total: ----", total,"\n"
                                      "----------------------------------------------------------------------------\n")
                        except:
                            if stdRoom < 1:
                                print("Please add a Standard Room")
                                main()
                            if dlxRoom < 1:
                                print("Please add a Deluxe Room")
                                main()
                            if prsRoom < 1:
                                print("Please add a Presidential Room")
                                main()

这是我试图从中获取房间数量的地方:

while True:
    print("\nMain Menu: \n"
          "1. Manage Room \n"
          "2. Booking \n"
          "3. View Available Rooms \n"
          "4. View Sales \n"
          "5. Exit \n")
    choose = int(input("Which menu would you like?: "))
    if choose == 1:
        print("Add and Repair Room \n"
        "1. Add a Room \n"
        "2. Repair a Room \n"
        "3. Return to Menu \n")
        manage = int(input("Input command: "))
        if manage == 1:
            print("Add a Room \n"
                  "1. Standard Room \n"
                  "2. Deluxe Room \n"
                  "3. Presidential Room \n")
            room = int(input("Input room type: "))
            if room == 1:
                stdRoom = int(input("Enter number of Standard Rooms: "))
                stdPrice = int(input("Enter price: "))
            elif room == 2:
                dlxRoom = int(input("Enter number of Deluxe Rooms: "))
                dlxPrice = int(input("Enter price: "))
            elif room == 3:
                prsRoom = int(input("Enter number of Presidential Rooms: "))
                prsPrice = int(input("Enter price: "))
            else:
                print("Invalid Input")
                main()

完整代码如果您需要它:

class exam:
    def main():
        personDisplay = 0
        person = 0
        days = 0
        sheetsAdd = 0
        towelAdd = 0
        bedAdd = 0

        while True:
            print("\nMain Menu: \n"
                  "1. Manage Room \n"
                  "2. Booking \n"
                  "3. View Available Rooms \n"
                  "4. View Sales \n"
                  "5. Exit \n")
            choose = int(input("Which menu would you like?: "))
            if choose == 1:
                print("Add and Repair Room \n"
                "1. Add a Room \n"
                "2. Repair a Room \n"
                "3. Return to Menu \n")
                manage = int(input("Input command: "))
                if manage == 1:
                    print("Add a Room \n"
                          "1. Standard Room \n"
                          "2. Deluxe Room \n"
                          "3. Presidential Room \n")
                    room = int(input("Input room type: "))
                    if room == 1:
                        stdRoom = int(input("Enter number of Standard Rooms: "))
                        stdPrice = int(input("Enter price: "))
                    elif room == 2:
                        dlxRoom = int(input("Enter number of Deluxe Rooms: "))
                        dlxPrice = int(input("Enter price: "))
                    elif room == 3:
                        prsRoom = int(input("Enter number of Presidential Rooms: "))
                        prsPrice = int(input("Enter price: "))
                    else:
                        print("Invalid Input")
                        main()


                elif manage == 2:
                    print("Repair a Room \n"
                          "Which room would you like to repair? \n"
                          "1. Standard Room \n"
                          "2. Deluxe Room \n"
                          "3. Presidential Room \n")
                    repair = int(input("Input command: "))
                    if repair == 1:
                        minus = int(input("How many rooms would you like to repair? "))
                        displayMinus = minus - stdRoom
                    elif repair == 2:
                        minus = int(input("How many rooms would you like to repair? "))
                        displayMinus = minus - dlxRoom
                    elif repair == 3:
                        minus = int(input("How many rooms would you like to repair? "))
                        displayMinus = minus - prsRoom
                    else:
                        print("Invalid Input")
                        continue
                    
                elif manage == 3:
                    continue
                    
                else:
                    print("Invalid Input")
                    continue


            if choose == 2:
                while stdRoom >= 1:
                    while dlxRoom >= 1:
                        while prsRoom >= 1:                            
                                    
                            try:
                                print("Booking \n"
                                      "Enter room you want to book: \n"
                                      "1. Standard --- Available: ", stdRoom,"\n"
                                      "2. Deluxe ----- Available: ", dlxRoom,"\n"
                                      "3. Presidential Available: ", prsRoom,"\n")
                                booking = int(input("Enter room you want to book: "))
                                if stdRoom < 1:
                                    print("Please add a room")
                                    main()
                                if booking == 1:
                                    stdRoom -= 1
                                    print("You have chosen Standard \n")
                                    fName = input("Enter first name: ")
                                    lName = input("Enter last name: ")
                                    days = int(input("Days of stay: "))
                                    addPerson = input("Would you like to add another person(P.100)? Y/N ")
                                    if addPerson == "Y" and "y":
                                        person = int(input("Enter number of persons: "))
                                        personDisplay = person * 100
                                    else:
                                        print("0 persons added")
                                    misc = input("Would you like to add extra bed sheets, towel or bed? Y/N ")
                                    if misc == "Y" and "y":
                                        print("1. Bed Sheets: 100 \n"
                                              "2. Towel: 50 \n"
                                              "3. Bed: 150 \n")
                                        ask = int(input("Enter misc additions: "))
                                        if ask == 1:
                                            sheets = int(input("Enter number of bed sheets: "))
                                            sheetsAdd = sheets * 100
                                        elif ask == 2:
                                            towel = int(input("Enter number of towel: "))
                                            towelAdd = towel * 100
                                        elif ask == 3:
                                            bed = int(input("Enter number of beds: "))
                                            bedAdd = bed * 100
                                        else:
                                            print("0 items added")
                                    else:
                                        print("0 items added")
                                        
                                    total = (days * stdPrice + personDisplay + sheetsAdd + towelAdd + bedAdd)
                                    print("Room Booking Sucessful: "
                                          "First Name: ", fName,"\n"
                                          "Last Name: ", lName,"\n"
                                          "Days of Stay: ", days,"\n"
                                          "Person's added: ",person,"Price: ",personDisplay,"\n"
                                          "Miscellaneous: \n"
                                          "Bed Sheets: ", sheetsAdd,"\n"
                                          "Towels: ", towelAdd,"\n"
                                          "Beds: ", bedAdd,"\n"
                                          "Total: ----", total,"\n"
                                          "----------------------------------------------------------------------------\n")
                            except:
                                if stdRoom < 1:
                                    print("Please add a Standard Room")
                                    main()
                                if dlxRoom < 1:
                                    print("Please add a Deluxe Room")
                                    main()
                                if prsRoom < 1:
                                    print("Please add a Presidential Room")
                                    main()
                        
            
            if choose == 5:
                break
book = exam
book.main()

标签: python

解决方案


可能只是将您在 while 循环中使用的三个变量(即 stdRoom、dlxRoom 和 prsRoom)初始化为代码顶部的 0。

class exam:
    def main():
        personDisplay = 0
        person = 0
        days = 0
        sheetsAdd = 0
        towelAdd = 0
        bedAdd = 0
        stdRoom = 0
        dlxRoom = 0
        prsRoom = 0
        

推荐阅读