Python 为什么我的函数总是出现UnboundLocalError?

Python 为什么我的函数总是出现UnboundLocalError?,python,function,Python,Function,所以我一直在努力完成作业的第一部分。出于某种原因,entry()函数中的while循环一直在导致问题。据我所知,这是唯一一个让我感到厌烦的声明。你能帮我修一下吗 #Satinderpal Saroa A3 30027872 #Entrance Function def entrance(): lockedDoor = True print(""" ROOM: ENTRANCE ____________________________________________

所以我一直在努力完成作业的第一部分。出于某种原因,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()

请在entrace中使用全局变量“lever”,否则在程序中使用全局变量“lever”

请在entrace中使用全局变量“lever”,否则在程序中使用全局变量“lever”

在修改变量的每个作用域中将变量声明为全局变量将变量声明为全局变量在每个要修改它们的范围中,dok。我现在就试试,告诉你发生了什么事。我只想补充一点,教授不允许我们使用全局变量。更新,不起作用,得到一个错误,说名称没有定义在某个地方,即使我很确定我定义了它。你在入口函数中定义了杠杆吗?是的,我今天早些时候就解决了所有问题。我使用“==”而不是“=”来将用户输入分配给需要从entry()函数更改的值。一切都很好!好啊我现在就试试,告诉你发生了什么事。我只想补充一点,教授不允许我们使用全局变量。更新,不起作用,得到一个错误,说名称没有定义在某个地方,即使我很确定我定义了它。你在入口函数中定义了杠杆吗?是的,我今天早些时候就解决了所有问题。我使用“==”而不是“=”来将用户输入分配给需要从entry()函数更改的值。一切都很好!