所以我一直在努力工作的第一部分。出於某種原因,入口()函數中的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()
變量聲明爲全球在他們被修改 –