意外的Unindent和语法错误

时间:2015-11-30 23:25:30

标签: python syntax

我对编程很新,并开始编写基于文本的游戏代码。

然而,我一直遇到两个错误;意外的Unindent和语法错误。

我正在使用try,但它告诉我except上的语法错误,如果我在except之后删除了整个块,那么它会给我一个“意外的Unindent”错误下一行(在“左门”之后),突出显示空白区域。

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

另外,我不会使用Try和Except运行脚本。我会使用函数,然后调用函数。

你看到代码的末尾我添加了return。这只是因为我运行程序时没有出错。你可以删除它。