python rpg中的缩进错误

2024-03-29 11:56:22 发布

您现在位置:Python中文网/ 问答频道 /正文

为了上课,我做了一个“生命中的一天”,我做了一个角斗士的一天。我有一个错误:

    if inp == "attack":
                  ^
    Indentation Error: unintended does not match any outer indentation level

我试过检查并修复所有的凹痕,但似乎找不到问题所在。我该怎么解决这个问题?还有什么改进的建议吗?来源如下:

http://pastebin.com/2RxbdzXw


Tags: ifmatch错误notanyerrorlevelouter
3条回答

看起来您将if缩进了4个空格,而您可能打算使用8。你知道吗

它没有用if Battle == 0:均匀地缩进

它不在while player.health ...块中

请记住,Python使用缩进表示代码层次结构,例如,在类C语言中,您将拥有:

if (....) {
   something
}

Python只会

if (...):
    something

你的意图完全不可靠:

                            while turn == 0:
                if inp == "attack":
                rnd1 = random.randint(1,2)
                    if rnd1 == 1:

应该是的

                            while turn == 0:
                                if inp == "attack":
                                    rnd1 = random.randint(1,2)
                                    if rnd1 == 1:

第236行应该进一步缩进到从第235行开始的while循环中 第237行应该缩进到第236行的if子句中

所以看起来应该更像

while turn == 0:  # line 235
    if inp == "attack":  # line 236
        rnd1 = random.randint(1,2)  # line 237
            if rnd1 == 1:

相关问题 更多 >