为什么我一直在使用我的函数获取UnboundLocalError?

时间:2016-11-16 06:14:59

标签: python function

所以我一直在努力完成作业的第一部分。由于某种原因,entry()函数中的while循环导致了问题。据我所知,这是唯一让我失望的声明。可以帮我解决这个问题吗?

#Satinderpal Saroa A3 30027872
#Entrance Function
def entrance():
    lockedDoor = True
    print("""
    ROOM: ENTRANCE
    _____________________________________________________________
    You are at the entrance. The door you entered through is gone
    and the door to what might be the exit is in front of you.
    There are two entryways to the left and the right, what will
    you choose to do?
    _____________________________________________________________
    Your choices are:
    1. Try to open the door
    2. Go through the left entryway
    3. Go through the right entryway
    """)
    enterChoice = int(input("Your choice: "))
    while lockedDoor == True:
        if (lever == "backwards" and dial == "red"):
            lockedDoor == False
            print("You go through the door and enter... another part of the house?!")
        if(enterChoice not in [1,2,3]):
            print("INVALID INPUT: Please enter 1,2, or 3.")
            enterChoice = int(input("Your choice: "))
        if (enterChoice == 1):
            print("You try to open the door, it doesn't move an inch!")
            enterChoice = int(input("Your choice: "))
        elif(enterChoice == 2):
            print("You make your way down to the kitchen.")
            lever = kitchen()
        elif(enterChoice == 3):
            print("You make your way down to the pantry")
            dial = pantry()
        return()

#Kitchen Function
def kitchen():
    lever = "nothing"
    print("""
    ROOM: KITCHEN
    _____________________________________________________________
    You reach the kitchen. Everything is spotless, a disturbing
    revelation as you know that the house hasn't been touched
    in decades. Near the sink, there is a lever that can be pulled
    backwards or forwards. What will you do?
    _____________________________________________________________
    Your choices are:
    1. Pull the lever backwards
    2. Push the lever forwards
    3. Do nothing and go back to the entrance
    """)
    kitchenChoice = int(input("Your choice: "))
    if (kitchenChoice not in [1,2,3]):
        print("INVALID INPUT: Please choose between 1,2, and 3")
        kitchenChoice = int(input("Your choice: "))
    elif (kitchenChoice == 1):
        lever == "backwards"
        print("The lever is pulled backwards")
        lever = entrance()
    elif (kitchenChoice == 2):
        lever == "forwards"
        print("The lever is pushed forwards")
        lever = entrance()
    elif (kitchenChoice == 3):
        print("You don't do anything and return to the entrance")
        lever = entrance()
    return (lever)

#Pantry Function
def pantry():
    dial = "nothing"
    print("""
    ROOM: PANTRY
    _______________________________________________________________
    You reach the pantry. All of the foodstocks and goods have been
    expired for ages. You notice a dial that can be turned to one of
    three colors; red, blue, and green. What will you do?
    _______________________________________________________________
    Your choices are:
    1. Turn the dial to red
    2. Turn the dial to blue
    3. Turn the dial to green
    4. Don't do anything and return to the entrance
    """)
    pantryChoice = int(input("Your choice: "))
    if (pantryChoice not in [1,2,3,4]):
        print("ERROR: Please enter 1,2,3 or 4.")
        pantryChoice = int(input("Your choice: "))
    elif (pantryChoice == 1):
        dial == "red"
        print("The dial is set to red")
        dial = entrance(dial)
    elif (pantryChoice == 2):
        dial == "blue"
        print("The dial is set to blue")
        dial = entrance()
    elif (pantryChoice == 3):
        dial = "green"
        print("The dial is set to blue")
        dial = entrance()
    elif (pantryChoice == 4):
        print("You don't do anything and you return to the entrance")
        dial = entrance()
    return(dial)

#Start function
def start():
    entrance()

start()

1 个答案:

答案 0 :(得分:0)

请在entrace中初始化'lever'varigbel,否则在程序中使用全局变量'lever'