压痕不一致

时间:2017-10-24 20:58:12

标签: python python-2.7 debugging

所以我一直在制作这个控制台文字"游戏"似乎有一个我无法解决的错误。它看起来像这样:

def left_room3():
    while True:
        i = 0;
        while i &lt len(weapons):
            print "One of your weapons are: " + weapons[i]
            i = i + 1
        yee = raw_input("You have encountered a zombie! What weapon do you use?\n")
        if yee == "sword":
            zombie.health -= sword.damage
            zombie.durability -= 1
            print "The zombie has taken " + str(sword.damage) + " damage."
            me.health -= zombie.damage
            print "You have taken " + str(zombie.damage) + " damage."
        elif yee == "gun":
            zombie.health -= gun.damage
            print "The zombie has taken " + str(gun.damage) + " damage."
            me.health - zombie.damage
            print "You have taken " + str(zombie.damage) + " damage."
            gun.durability -= 1
        else:
            print "That is not an option. Please try again."
        if zombie.health &lt= 0:
            print "You won against the zombie!"
            print "Your save code: 08927672. Enter this at the start to come back to this point."
            room4()
            break
        elif me.health &lt= 0:
            print "You died!"
            exit(0)

所以它似乎给了我这个错误:

Error: Inconsistent indentation detected!
1) Your indentation is outright incorrect (easy to fix) OR
2) Your indentation mixes tabs and spaces.

我使用IDLE,它提供了更清晰的错误信息。

它强调了这一行代码:

    i = 0;

但我不知道这是否真的导致问题的代码...... (我已经看到IDE /编译器突出显示一段代码的情况,但是它导致了这个问题的不同部分)

我还有其他几乎完全相同的代码块,但IDLE并没有突出显示它们(也许是因为它首先检查了阻塞?我不知道,但是我觉得IDLE非常错误将从下到上检查(因为代码从上到下),但它可能是可能的。)

1 个答案:

答案 0 :(得分:1)

您强烈建议您error混合使用spacestabs。在大多数其他编程语言中,缩进影响code的运行方式,只是让它更容易理解。

这在Python中是不同的,其中所有scopes(我认为这是正确的术语)所以if-statementsloops等由行的缩进级别定义。

因此,您需要在整个代码中一致,了解您使用的缩进类型 - 所以1标签,4空格或其他并坚持下去!否则,编译器将不知道该行所在的scope以及何时退出该部分。

很难从你的问题代码中看到你确切错误的哪一行,但这很可能是导致问题的原因所以你应该仔细看看你的实际代码。

如果您无法通过查找找到它,大多数文本编辑都可以选择'highlight / show white-space and tab',这将使错误的来源脱颖而出。

希望这有帮助!

相关问题