2015-11-30 29 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 

此外,使用嘗試和除我不會運行該腳本。我會使用函數,然後再調用函數。

您在代碼的末尾看到我添加了返回。這只是當我運行程序時沒有出現錯誤。你可以刪除它。