【问题标题】:Unexpected Unindent and Syntax errors意外的取消缩进和语法错误
【发布时间】:2016-03-04 18:07:06
【问题描述】:

我对编程还很陌生,并且已经开始编写基于文本的游戏。

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

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

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

【问题讨论】:

  • 查看try的缩进。查看except 的缩进。这些行的缩进看起来有什么问题吗?

标签: python syntax


【解决方案1】:

您捕获异常的行需要缩进与您的尝试相同的级别

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

【讨论】:

    【解决方案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 来运行脚本。我会使用函数,然后再调用函数。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-04
      • 2011-09-14
      • 2021-05-28
      • 2011-04-24
      • 2018-08-16
      • 1970-01-01
      • 2012-10-25
      • 1970-01-01
      相关资源
      最近更新 更多