语法和缩进错误的问题

时间:2014-12-24 15:43:22

标签: python

我正在浏览一本书,在本书的这一点上,要求我制作一个称为函数的小型视频游戏,使用ifwhile& #39; s - 到目前为止本书涵盖的所有内容基本上都是如此。但是,我在这部分代码中遇到了这个错误:

代码已修改,收到新错误。

    File "ex35_study.py", line 24
third_scenario_code()
IndentationError: unindent does not match any outer indentation level

这是我的代码:

     options_thirdscenario_actions = ['Examine the door', 'Try to force it']

def third_scenario_code():
    print "Let me try to crack this thing up, says Lars as he starts to type in the panel. You hear the sounds of the fight out there, there's not much time left. "
    print "After receiving several commands a window with a code pop ups. "
    print codefile.read() 


def third_scenario():
    print "You two get out of the cell and approach to the exit, a long corridor is ahead of you, flashing red lights indicate an state of emergency, you must evacuate."
    print "As soon as you two approach to the door, it closes"
    print "Crap it must be the emergency system, we have been detected"
    next = raw_input("What do you do> ")
if next == 'Examine the door':
    print "A small panel comes out, requires to enter a code of words"
    third_scenario_code()
elif next == 'Try to force it':
    print "You try to force the door with no result"
    print options_thirdscenario_actions    
    next2 = raw_input("What else do you do> " )
    if next2 = 'Examine the door'
        third_scenario_code()
else:
    print "You already did that"

我在整个程序中遇到类似的错误,我怀疑它与缩进有关,但我已经尝试了我在google中看到的每个建议都没有取得丰硕成果。提前谢谢。

2 个答案:

答案 0 :(得分:1)

您在if条件之一后缺少冒号,并且需要排列相同范围的内容,即函数调用后的打印,但您也可能混合使用空格和制表符。建议始终使用4个空格而不是制表符,并且可以为此设置大多数编程编辑器。

我还建议抓住pylint并使用它。它将帮助您发现许多潜在的错误,并帮助您养成良好的习惯。

答案 1 :(得分:0)

由于third_scenario_code()缩进,您需要在print下撰写。

更改以下内容:

if next == 'Examine the door':
            print "A small panel comes out, requires to enter a code of words"
                third_scenario_code()

到:

if next == 'Examine the door':
            print "A small panel comes out, requires to enter a code of words"
            third_scenario_code()