意外的Unindent和语法错误

2024-04-24 14:16:33 发布

您现在位置:Python中文网/ 问答频道 /正文


Tags: python
2条回答

你看到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。这只是为了在我运行程序时不会出错。你可以移除它。你知道吗

捕获异常的行需要在与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()

这个例子显示了我的意思:https://wiki.python.org/moin/HandlingExceptions

相关问题 更多 >