I'm fairly new to programming and have started to code a text based game.
However, I keep coming across two errors; Unexpected Unindent and Syntax error.
I am using try
but it tells me a Syntax error on except
and if I get rid of the whole chunk after except
it then gives me an "Unexpected Unindent" error on the next line (after "left door"), highlighting blank space.
def prompt_chouseroom1():
prompt_2 = raw_input ("You know what to do by now: ")
try:
if prompt_2 == ("Look around the room"):
print ("You see two doors; one on the left, and one on the right. There is also a dead body in the middle of the room")
print promt_chouseroom1 ()
if prompt_2 == ("Go through left door"):
left_door()
if prompt_2 == ("Go through middle door"):
prinnt ("WHAT?! You walked through the wall!")
middle_door()
if prompt_2 == ("Go through right door"):
right_door()
if prompt_2 == ("Look under the rug"):
print ("Oh my... you... you weren't supposed to find that. Well... looks like you can leave already. You win. Congrats... I guess :( ")
win_game()
else:
print ("Try to LOOK AROUNF THE ROOM.... just a hint ;)")
print
prompt_chouseroom1()
except ValueError:
print ("Try to LOOK AROUNF THE ROOM.... just a hint ;)")
print
prompt_chouseroom1()
def left_door():
Your line to catch the exception needs to be indented at the same level as your try
try:
if prompt_2 == ("Look around the room"):
print ("You see two doors; one on the left, and one on the right. There is also a dead body in the middle of the room")
print promt_chouseroom1 ()
if prompt_2 == ("Go through left door"):
left_door()
if prompt_2 == ("Go through middle door"):
prinnt ("WHAT?! You walked through the wall!")
middle_door()
if prompt_2 == ("Go through right door"):
right_door()
if prompt_2 == ("Look under the rug"):
print ("Oh my... you... you weren't supposed to find that. Well... looks like you can leave already. You win. Congrats... I guess :( ")
win_game()
else:
print ("Try to LOOK AROUND THE ROOM.... just a hint ;)")
print
prompt_chouseroom1()
except ValueError:
print ("Try to LOOK AROUND THE ROOM.... just a hint ;)")
print
prompt_chouseroom1()
This example shows what I mean: https://wiki.python.org/moin/HandlingExceptions
Do you see how try:
is indented? You need to match the same indentation level with except:
Here the codes is fixed
def prompt_chouseroom1():
prompt_2 = raw_input ("You know what to do by now: ")
try:
if prompt_2 == ("Look around the room"):
print ("You see two doors; one on the left, and one on the right. There is also a dead body in the middle of the room")
print promt_chouseroom1 ()
if prompt_2 == ("Go through left door"):
left_door()
if prompt_2 == ("Go through middle door"):
print ("WHAT?! You walked through the wall!")
middle_door()
if prompt_2 == ("Go through right door"):
right_door()
if prompt_2 == ("Look under the rug"):
print ("Oh my... you... you weren't supposed to find that. Well... looks like you can leave already. You win. Congrats... I guess :( ")
win_game()
else:
print ("Try to LOOK AROUNF THE ROOM.... just a hint ;)")
print
prompt_chouseroom1()
except ValueError:
print ("Try to LOOK AROUNF THE ROOM.... just a hint ;)")
print
prompt_chouseroom1()
def left_door():
return
Also, I wouldn't run the script using Try and Except. I would use functions, then later call the functions.
You see at the end of the code I added return. This was just so it didn't give an error when I ran the program. You can remove it.
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.