-3

I have been trying to use try and except to counter this problem but it seems it does not work, The idea is to return from the start if my code does not have rooms available or the user haven't input the number yet but it just errors. Here is my problem with the code:

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

This is where I'm trying to get the number of rooms from:

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

FULL CODE If you need it:

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()
You
  • 1
  • 3
  • You are taking the stdRoom input inside some condition and when the flow doesn't enter this condition to accept the input, it gives this error when you use it as "stdRoom" is not defined and it is being used – Simplecode Nov 19 '20 at 11:49
  • What do I then? Any advice? – You Nov 19 '20 at 12:39
  • 1
    Does this answer your question? ["UnboundLocalError: local variable referenced before assignment" after an if statement](https://stackoverflow.com/questions/15367760/unboundlocalerror-local-variable-referenced-before-assignment-after-an-if-sta) – Tomerikoo Nov 19 '20 at 12:49
  • Imagine what happens if I choose the menu for `Booking` first. The code goes directly to the `if chosse == 2` part where it tries to read `stdRoom`, but it is only defined under `if choose == 1` (which the code never reached) – Tomerikoo Nov 19 '20 at 12:51

1 Answers1

0

may be just initialise the three vairables you are using in while loop i.e stdRoom, dlxRoom and prsRoom to 0 at the top of your code.

class exam:
    def main():
        personDisplay = 0
        person = 0
        days = 0
        sheetsAdd = 0
        towelAdd = 0
        bedAdd = 0
        stdRoom = 0
        dlxRoom = 0
        prsRoom = 0
        
Simplecode
  • 559
  • 7
  • 19