2016-11-16 56 views
1

所以我一直在努力工作的第一部分。出於某種原因,入口()函數中的while循環一直在引發問題。據我所知,這是唯一一直在拋棄我的聲明。可以幫我解決這個問題嗎?爲什麼我一直使用我的函數獲取UnboundLocalError?

#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() 
+0

變量聲明爲全球在他們被修改 –

回答

0

,因爲它是在它使用的程序也可以使用一個全局變量「槓桿」

+0

確定每個範圍,請初始化entrace「槓桿」 variabel 。我現在就試試看,告訴你發生了什麼事。只是想補充說教授不允許我們使用全局變量。 –

+0

更新,不起作用,得到一個錯誤,說這個名字沒有定義在某個地方,儘管我很確定我確定了它 –

+0

你是否在入口函數中定義了這個槓桿? –

相關問題