2015-11-30 28 views
1

我对编程相当陌生,并开始编写基于文本的游戏。意外的Unindent和语法错误

但是,我不断遇到两个错误;意外的Unindent和语法错误。

我使用try但它告诉我一个语法错误上except,如果我得到except后摆脱了整个块的它,然后给我的下一行“意外取消缩进”错误(“左门”后)突出空白空间。

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(): 
+1

看看'try'的缩进。看看'except'的缩进。这些线条的缩进是否有任何问题? – user2357112

回答

0

你钓异常需要在同一水平缩进为您尝试

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() 

这个例子说明我的意思:https://wiki.python.org/moin/HandlingExceptions

0

你怎么看try:缩进?你需要配合except: 这里的代码相同的缩进级别是固定的

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 

此外,使用尝试和除我不会运行该脚本。我会使用函数,然后再调用函数。

您在代码的末尾看到我添加了返回。这只是当我运行程序时没有出现错误。你可以删除它。