缩进预期?

Indent Expected?

我是 python 的新手,正在做一个小型的文字冒险,直到现在进展顺利 我目前正在实现一个剑系统,如果你有一定大小的剑,你可以杀死某些人大小的怪物。我正在尝试编写另一个怪物遭遇的代码并且我已经编写了剑的东西但是我试图用 elseif...elif...elif 语句来完成它,即使我在正确的缩进中它仍然说 indent expected I don't know what to do here's the code:

print ('you find a monster about 3/4 your size do you attack? Y/N')
yesnotwo=input()
if yesnotwo == 'Y':
    if ssword == 'Y':
        print ('armed with a small sword you charge the monster, you impale it before it can attack it has 50 gold')
        gold += 50
        print ('you now have ' + str(gold) + ' gold')
    elif msword == 'Y':
        print ('armed with a medium sword you charge the monster, you impale the monster before it can attack it has 50 gold')
        gold += 50
        print ('you now have ' + str(gold) + ' gold')
    elif lsword == 'Y':
        print ('armed with a large broadsword you charge the beast splitting it in half before it can attack you find 50 gold ')
        gold += 50
        print ('you now have ' + str(gold) + ' gold')
    else:

事实上,关于 Python 中的缩进,您需要了解很多事情:

Python 非常关心缩进。

在其他语言中,缩进不是必需的,只是为了提高可读性。在Python中,缩进是必要的,并替代其他语言的关键字begin / end{ }

这是在代码执行之前验证的。因此,即使永远达不到缩进错误的代码,也不行。

有不同的缩进错误,阅读它们有很大帮助:

1. IndentationError: expected an indented block

出现此类错误的原因有多种,但常见的原因是:

  • 您有一个 : 下面没有缩进块。

这里有两个例子:

例1,无缩进块:

输入:

if 3 != 4:
    print("usual")
else:

输出:

  File "<stdin>", line 4

    ^
IndentationError: expected an indented block

输出表明您需要在第 4 行的 else: 语句之后有一个缩进块。

例2,无缩进块:

输入:

if 3 != 4:
print("usual")

输出

  File "<stdin>", line 2
    print("usual")
        ^
IndentationError: expected an indented block

输出表明您需要在第 2 行的 if 3 != 4: 语句之后有一个缩进块。

2。 IndentationError: unexpected indent

缩进块很重要,但只有应该缩进的块。此错误表示:

- 您有一个缩进块,前面没有 :

示例:

输入:

a = 3
  a += 3

输出:

  File "<stdin>", line 2
    a += 3
    ^
IndentationError: unexpected indent

输出表明它不希望第 2 行出现缩进块。您应该通过删除缩进来解决此问题。

3。 TabError: inconsistent use of tabs and spaces in indentation

  • 但基本上是这样,您在代码中使用了制表符和空格。
  • 你不想要那个。
  • 删除所有制表符并将它们替换为四个空格。
  • 并将您的编辑器配置为自动执行此操作。
  • 您可以获得更多信息here


最后,回到你的问题:

I have it in the right indentation it still says indent expected I don't know what to do

只需查看错误的行号,并使用之前的信息进行修复。