hi beginner here I'm learning if statement
this my code
print("Welcome to the dungeon.")
print("Your mission is to find the ursine sword.")
level_1 = input("there is two doors infront of you type 'right' for the right door and 'left' for the left door.\n" )
level_1.lower()
if level_1 == "right" :
print("this door is closed your level is too low to open it.\n")
if level_1 == "left" :
level_2 = input("you found a two ladder one leads to upstairs type 'upstairs' and another leads to the basement type 'basement'.\n ")
level_2.lower()
if level_2 == "basement" :
print("hisssssss , you got bitten by snake game over")
if level_2 == "upstairs" :
level_3 =int(input("it seems like the final room thre is three swords infront of you two of them must be traps chose '1','2' or '3' "))
if level_3 == 1 :
print("you released a powerful ghost you'r dead")
elif level_3 == 3 :
print("you opened a portal to a cater you're dead")
elif level_3 == 2 :
print("congratulations you found the ursine sword")
else :
print("game over.")
I tried many other ways to use input but can't describe it the issue is level_2 is undefined when i choose 'right' but if i choose 'left' the code continue with no problem and i can choose 'upstairs' with no issue and continue with level_3 and can choose form 1,2,3 with no issue even though level_3 is inputted like level_2 or am I wrong? #new code
print("Welcome to the dungeon.")
print("Your mission is to find the ursine sword.")
print("Welcome to the dungeon.")
print("Your mission is to find the ursine sword.")
level_1 = input("there is two doors infront of you type 'right' for the right door and 'left' for the left door.\n")
if level_1== "right":
print("this door is closed your level is too low to open it.\n")
elif level_1== "left":
level_2 =input("you found a two ladder one leads to upstairs type 'upstairs' and another leads to the basement type 'basement'.\n ").lower()
if level_2== "basement":
print("hisssssss , you got bitten by snake game over")
elif level_2== "upstairs":
level_3 = int(input("it seems like the final room thre is three swords infront of you two of them must be traps chose '1','2' or '3' "))
if level_3 == 1 :
print("you released a powerful ghost you'r dead")
elif level_3 == 3 :
print("you opened a portal to a cater you're dead")
elif level_3 == 2 :
print("congratulaitons you found the ursine sword")
else :
print("game over.")
this the new code the indentation fixed my issue
Your level_2
is only defined if level_1
is 'left'. If it's not left then level_2 is not defined at all which throws the error.
Imagine if my level_1 is not "left", then when I get to the line which says level_2.lower()
then python doesn't know what the value of level_2 is because it's never defined so it can't run the .lower()
function
What you have to do is assign an initial value to level_2 before the if checks begin so that you dont get that error
level_2 = ""
level_1 = input("there is two doors infront of you type 'right' for the right door and 'left' for the left door.\n" )
level_1.lower()
if level_1 == "right" :
print("this door is closed your level is too low to open it.\n")
if level_1 == "left" :
level_2 = input("you found a two ladder one leads to upstairs type 'upstairs' and another leads to the basement type 'basement'.\n ")
level_2.lower()
The problem is that level_2
is only being given a value if you choose left in level_1
. If you choose right, it prints this door is closed your level is too low to open it.
but then level_2
stays undefined so it raises the error. You could do the above answer, or you could add a while
loop that runs until level_1
is left.
print("Welcome to the dungeon.")
print("Your mission is to find the ursine sword.")
level_1 = ''
while level_1 != 'left':
level_1 = input("there is two doors infront of you type 'right' for the right door and 'left' for the left door.\n")
level_1.lower()
if level_1 == 'right':
print("this door is closed your level is too low to open it.\n")
else:
pass
level_2 = input("you found a two ladder one leads to upstairs type 'upstairs' and another leads to the basement type 'basement'.\n ")
level_2.lower()
if level_2 == "basement" :
print("hisssssss , you got bitten by snake game over")
if level_2 == "upstairs" :
level_3 =int(input("it seems like the final room thre is three swords infront of you two of them must be traps chose '1','2' or '3' "))
if level_3 == 1 :
print("you released a powerful ghost you'r dead")
elif level_3 == 3 :
print("you opened a portal to a cater you're dead")
elif level_3 == 2 :
print("congratulations you found the ursine sword")
else :
print("game over.")
You get an error because level_2
only gets defined when level_1
is "left". If level_1
is not "left", your code will continue to throw errors.
What you can do is run lower_2.upper()
in the second if statement. That way you only call the upper method on lower_2
after its defined. For example,
if level_1 == "left" :
level_2 = input("you found a two ladder one leads to upstairs type 'upstairs' and another leads to the basement type 'basement'.\n ")
level_2.lower()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.