缩进预期?

10m*_*its -2 python indentation

我是一个新的python和一个小文本冒险,它一直进展到现在我正在实施一个剑系统,如果你有一定尺寸的剑,你可以杀死一定大小的怪物.我试图代码中的另一个怪物的遭遇和我已经编写了剑的东西,但我想与完成它elseif...elif...elif说法,即使我有它的右缩进还在说缩进预计我不知道这里做的是代码:

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:
Run Code Online (Sandbox Code Playgroud)

Xav*_* C. 8

实际上,您需要了解Python中缩进的多项内容:

Python非常关心缩进.

在许多其他语言中,缩进不是必需的,但提高了可读性.在Python缩进中替换关键字begin / end{ }因此是必要的.

这在代码执行之前得到验证,因此即使具有缩进错误的代码永远不会到达,它也无法工作.

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

1."IndentationError:预期缩进块"

它们是导致此类错误的多种原因,但常见原因是:

  • 你有一个":",后面没有缩进块.

这是两个例子:

例1,没有缩进块:

输入:

if 3 != 4:
    print("usual")
else:
Run Code Online (Sandbox Code Playgroud)

输出:

  File "<stdin>", line 4

    ^
IndentationError: expected an indented block
Run Code Online (Sandbox Code Playgroud)

输出表明您需要在IndentationError: expected an indented block语句后面有一个缩进的块行4

例2,未缩进块:

输入:

if 3 != 4:
print("usual")
Run Code Online (Sandbox Code Playgroud)

产量

  File "<stdin>", line 2
    print("usual")
        ^
IndentationError: expected an indented block
Run Code Online (Sandbox Code Playgroud)

输出表明您需要在:语句后面有一个缩进的块行2

2."IndentationError:意外缩进"

缩进块很重要,但只有缩进块才有.所以基本上这个错误说:

- 你有一个缩进的块,前面没有":".

例:

输入:

a = 3
  a += 3
Run Code Online (Sandbox Code Playgroud)

输出:

  File "<stdin>", line 2
    a += 3
    ^
IndentationError: unexpected indent
Run Code Online (Sandbox Code Playgroud)

输出表明他不期望缩进块行2,然后你应该删除它.

3."TabError:缩进中标签和空格的使用不一致"

  • 你可以在这里获得一些信息.
  • 但基本上,你在代码中使用制表符和空格.
  • 你不希望这样.
  • 删除所有选项卡并将其替换为四个空格.
  • 并配置您的编辑器自动执行此操作.


最后,回到你的问题:

我有正确的缩进它仍然说缩进预期我不知道该怎么做

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