Python:“预期的缩进块”不知道如何修复

时间:2014-03-09 18:31:23

标签: python

好的,所以我有我的程序,但我得到一个“预期的缩进块”,我不知道它在哪里,我相信我说得对,但我很困惑。

##Cave fuction
def cave():

    global lvl
    global mhp
    global exp

    while True:
        print("Who whould you like to talk to. (Emily(1), James(2), Paco(3)")
        talk = int(input("Who to talk to: "))
        ptint(" ")               
        if talk == 1:
            #put storie function here
        elif talk == 2:
            #put train function here
        elif talk == 3:
            print("Your level is", lvl, "you have", mhp, "and have", exp, "EXP")
        else:
            amsterdam = 7 #filler

        print("Anthing else needed(y/n)")
        ant = input("Anthing: ")

        if ant == n:
            break
        else:
            mexico = 19 #filler

7 个答案:

答案 0 :(得分:2)

在if / elif语句之间放置pass(或print语句或真正执行的任何内容)。这些评论并不是真正的代码。

答案 1 :(得分:2)

在Python中,C中没有像{}那样的空块。如果你想阻止什么都不做,你必须使用pass关键字。例如:

if talk == 1:
    pass  # Put storie function here.
elif talk == 2:
    pass  # Put storie function here.

这应该可以解决您的问题。在以:结尾的行之后,必须使用下一行,并且在这方面注释不计入缩进。

答案 2 :(得分:1)

你必须在if-else语句中放入一些有效的语句,你写的是put storie function here。以下代码不会抛出错误,因为每个if-else都有一些有效的声明:

def cave():

    global lvl
    global mhp
    global exp

    while True:
        print("Who whould you like to talk to. (Emily(1), James(2), Paco(3)")
        talk = int(input("Who to talk to: "))
        ptint(" ")               
        if talk == 1:
            #put storie function here
            pass
        elif talk == 2:
            #put train function here
            pass
        elif talk == 3:
            print("Your level is", lvl, "you have", mhp, "and have", exp, "EXP")
        else:
            amsterdam = 7 #filler

        print("Anthing else needed(y/n)")
        ant = input("Anthing: ")

        if ant == n:
            break
        else:
            mexico = 19 #filler

答案 3 :(得分:1)

ifelif的条件之后,在else之后,预计会出现缩进块。像这样:

if condition:
    indented_block
elif condition:
    indented_block
else:
    indented_block

只是作为一个块的评论:

if condition:
    # ...
不考虑

,所以你必须把东西放在那里。另一种方法是使用占位符:

if condition:
    pass

pass是一个空操作。执行时没有任何反应。

答案 4 :(得分:0)

如果这是你正在运行的确切代码,那么你的问题应该在:

while True:
    print("Who whould you like to talk to. (Emily(1), James(2), Paco(3)")
    talk = int(input("Who to talk to: "))
    ptint(" ")               
    if talk == 1:
        #put storie function here
    elif talk == 2:
        #put train function here

这里你得到的注释#put storie function here没有被评估为代码所以基本上python解释的是:

while True:
    print("Who whould you like to talk to. (Emily(1), James(2), Paco(3)")
    talk = int(input("Who to talk to: "))
    ptint(" ")               
    if talk == 1:
    elif talk == 2:
    […]

虽然它期望:

while True:
    print("Who whould you like to talk to. (Emily(1), James(2), Paco(3)")
    talk = int(input("Who to talk to: "))
    ptint(" ")               
    if talk == 1:
        print("python wants actual code here in an inner block")
    elif talk == 2:
        print("python wants actual code here in an inner block as well")
    […]

因而无法编译

答案 5 :(得分:0)

你的if块是空的。试试这个

if talk == 1: pass #put storie function here elif talk == 2: pass #put train function here elif talk == 3: print("Your level is", lvl, "you have", mhp, "and have", exp, "EXP") else: amsterdam = 7 #filler

答案 6 :(得分:0)

你需要在之前输入一些声明。 elif talk == 2:

if talk == 1:
    print 1
elif talk == 2: