Python - 从文件中获取缩进错误

Python - Getting an Identation error out of the file

提问人:Thibault de Villèle 提问时间:10/29/2015 最后编辑:Thibault de Villèle 更新时间:10/29/2015 访问量:46

问:

我正在尝试用python制作一个小程序,没有什么复杂的,但非常长。刚才,我修改了“if”语句的一个条件,然后通过 MacOS 终端运行它,因为它有一个 Python shell / IDLE(我不知道它在英文中是如何称呼的)。

我的文档正好有 456 行,我得到的“IndentationError”位于“第 457 行”,这是输出:

文件“/Volumes/my usb key/MAIN_0.3.py”,第 457 行

                    ^

IndentationError:预期为缩进块

我真的不知道该怎么做,因为我的程序的最后一行都是评论。如果你想要一些代码,我会尽量为大家简单地(用英语)表达,但我想先知道在“终端”中运行它是否没有问题,因为它真的很奇怪。

编辑:这是一些代码:

#coding= utf-8
#here I put a lot of variables(38), which are all 'int' or 'randint(x, y)'
#then I define some functions, which are all calculus
#here is the main loop :
while (a!=10) and (b > 10):
    function_a(a)
    c=0
    while (c != 1) and (b > 0):
        while b != 0:
            print "\n"
            d=randint(0,20)
            if 0 <= d <= 3: #commentary explaining my program for my buddies at school
                b=function_b(b, e, f, g, h, i, j, a, k, l)
                if b <= 0: #commentary
                    m += n
                    p += a_b
                    if p > 1000: #commentary
                        p -= 1000
                        k += 1
                        print " BRAVO !"
                        print " now", k
                        time.sleep(1)
                        b += b_levelup
                        e += e_levelup
                        f += f_levelup
                        print " you won", b_levelup, "points"
                        time.sleep(1)
                        print " you won", e_levelup, "points"
                        time.sleep(1)
                        print " you won", f_levelup, "points"
                        time.sleep(1)
                print "\n"*2

            if 3 < d <= 8: #commentary

                b=function_b(b, e, f, g, q, r, s, a, k, t)

                if b <= 0: #commentary
                    m += u
                    p += v

                    if p > 1000: #
                        p -= 1000
                        k += 1
                        print " BRAVO !"
                        print " now", k
                        time.sleep(1)
                        b += b_levelup
                        e += e_levelup
                        f += f_levelup
                        print " you won", b_levelup, "points"
                        time.sleep(1)
                        print " you won", e_levelup, "points"
                        time.sleep(1)
                        print " you won", f_levelup, "points"
                        time.sleep(1)
                print "\n"*2

            if 8 < d <= 14: #commentary

                b=function_b(b, e, f, g, w, x, y, a, z, k, a_a)

                if b <= 0: #commentary
                    m += n
                    p += a_b
                    if p > 1000: #commentary
                        p -= 1000
                        k += 1
                        print " BRAVO !"
                        print " now", k
                        time.sleep(1)
                        b += b_levelup
                        e += e_levelup
                        f += f_levelup
                        print " you won", b_levelup, "points"
                        time.sleep(1)
                        print " you won", e_levelup, "points"
                        time.sleep(1)
                        print " you won", f_levelup, "points"
                        time.sleep(1)
                print "\n"*2

            if 15 <= d <= 16:#commentary
                a_c += 1
                function_c()

            if d == 17: #commentary
                a_d += 1
                function_d()

            #boss commentary
            if (d == 18) and (a_d == 0):
                print " mkay"
                time.sleep(2)
                print " nope"
                print " next door"

            #commentary
            if (d == 18) and (a_d >= 1):
                print " arrived"
                time.sleep(2)
                print " wanna go ?"
                print " yes - 1"
                print " No - any other"
                a_e=input(" ")
                if a_e == 1:
                    #boss commentary
Python macOS 缩进

评论

2赞 McGlothlin 10/29/2015
确保您使用制表符或空格,而不是两者的组合。
0赞 tuxtimo 10/29/2015
我们需要代码。最有趣或错误之前的一些行。如果你的文件是,你应该考虑把它分成多个部分吗?您使用的是制表符还是空格?还是混合?始终如一!nothing complicated, but extremely long
1赞 user2357112 10/29/2015
这意味着你有一个悬而未决的语法结构,比如一个没有或。tryexceptfinally
2赞 TigerhawkT3 10/29/2015
当然,我们需要看到一些代码。当你问机械师你的车怎么了,你会把车带到店里,对吧?
1赞 Prune 10/29/2015
你发布的代码有一个悬而未决的“if”语句:它没有正文。这将产生缩进错误。这与您的完整计划相匹配吗?最后一个活动代码(不是注释)是什么?

答:

1赞 user2357112 10/29/2015 #1

评论不是陈述,所以当你这样做时

if something:
    # Comment

没有身体。这是一个语法错误。如果你想要一个什么都不做的占位符正文,这就是该语句的用途。ifpass

if something:
    # Comment
    pass