当我运行代码并选择(输入)1或2时,我的变量出现错误,它表明我的变量未正确声明。 问题是,当我尝试在方法内部增加变量值时,它只是在该方法内部增加,而不是不计入其他方法(我想),因为我真的看不到任何其他原因
import time #importing the package to use it in slowing time
def printAll(phrase): #this method to just slow down my messages
time.sleep(2) #freezing time for 2 seconds
print(phrase) #then printing whatever i wanna print
printAll("To your right is a dark cave.") #just to clarify
printAll("In your hand you hold your trusty dagger.") #just to clarify
caveVisits = 0 #increases Every time i use the cave method (i disabled it for now)
houseVisits = 0 #expected to increase by one Every time i use the house method
overPowerSword = False #It's False until i enter the cave method
firstChoice = None #this is where i store my input (1 or 2)
def giveOptions(): #This method pretty much runs everything here
firstChoice = None #making sure it's already None to avoid any Error
printAll("Enter 1 to knock on the door of the house.") #1 for the house
printAll("Enter 2 to peer into the cave.") #2 for the cave
firstChoice = input() #assigning the value from null no the current input
if firstChoice == "2": #if it's 2 then we are going to cave
caveOption()
elif firstChoice == "1": #if 1 then we are entering the house
houseOption() #calling houseOption, it's written bellow
elif firstChoice != "2" or firstChoice != "1": #if either both
printAll("You entered " + firstChoice + " Please enter 1 or 2")#just to clarify
def houseOption(): #to enter the house
houseVisits +=1 #increases everytime we use it (The problem is here in
//case of input 1)
if overPowerSword == true: #if we already have the sword then it's true
printAll("You killed the dragon ") # just to clarify
else:
printAll("The dragon burned you") #if we don't have it yet
def caveOption(): #to enter the cave
if overPowerSword == False: #if the sword var is not true yet (The problem here in case of input 2)
overPowerSword = True # then make it true
printAll("You discard your silly old dagger and take the sword with you.") #just to clarify
printAll("You walk back out to the field.") #just to clarify
giveOptions #we use it for another input which this time actually
#triggers the house method to kill the dragon
else: #if we already claimed the sword
printAll("There isn't anything to do here anymore") #just to clarify
printAll("Getting out from the cave") #just to clarify
giveOptions
giveOptions() # we have to call the method so the code actually begins running
In case of input = 1 the error is : #This is the Error when i input 1
Traceback (most recent call last):
File "main.py", line 58, in <module> #error at line 58
giveOptions()
File "main.py", line 26, in giveOptions #26
houseOption()
File "main.py", line 35, in houseOption #35
houseVisits +=1 #not increasing by one
UnboundLocalError: local variable 'houseVisits' referenced before assignment
#That's because the houseVisits variable was increasing inside houseOption method and but not inside the other methods
In case of 2: #this is the error when i input 2
Traceback (most recent call last): #if i input 2
File "main.py", line 58, in <module> #error at line 51
giveOptions() #problem with the caveOption method
File "main.py", line 24, in giveOptions #at 24
caveOption() #problem with the caveOption method
File "main.py", line 44, in caveOption #at 44
UnboundLocalError: local variable 'overPowerSword' referenced before assignment #That's because the OPS variable was increasing inside the caveMethod method and but not inside the other methods i guess