Python 缩进预期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39395226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:12:16  来源:igfitidea点击:

Indent Expected?

pythonindentation

提问by 10midgits

I'm sort of new to python and working on a small text adventure it's been going well until now I'm currently implementing a sword system where if you have a certain size sword you can slay certain size monsters. I'm trying to code another monster encounter and I have coded the sword stuff but I'm trying to finish it off with an elseto the if...elif...elifstatement and even though I have it in the right indentation it still says indent expected I don't know what to do here's the code:

我对 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:

回答by Xavier C.

There are, in fact, multiple things you need to know about indentation in Python:

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

Python really cares about indention.

Python 真的很关心缩进。

In other languages, indention is not necessary but only serves to improve the readability. In Python, indentation is necessary and replaces the keywords begin / endor { }of other languages.

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

This is verified before the execution of the code. Therefore even if the code with the indentation error is never reached, it won't work.

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

There are different indention errors and you reading them helps a lot:

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

1. IndentationError: expected an indented block

1. IndentationError: expected an indented block

There are multiple reasons why you can have such an error, but the common reason will be:

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

  • You have a :without an indented block underneath.
  • :下面有一个没有缩进的块。

Here are two examples:

这里有两个例子:

Example 1, no indented block:

示例 1,无缩进块:

Input:

输入:

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

Output:

输出:

  File "<stdin>", line 4

    ^
IndentationError: expected an indented block

The output states that you need to have an indented block on line 4, after the else:statement.

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

Example 2, unindented block:

示例 2,未缩进的块:

Input:

输入:

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

Output

输出

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

The output states that you need to have an indented block on line 2, after the if 3 != 4:statement.

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

2. IndentationError: unexpected indent

2. IndentationError: unexpected indent

It is important to indent blocks, but only blocks that should be indented. This error says:

缩进块很重要,但只有应该缩进的块。这个错误说:

- You have an indented block without a :before it.

- 你有一个没有:前面的缩进块。

Example:

例子:

Input:

输入:

a = 3
  a += 3

Output:

输出:

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

The output states that it wasn't expecting an indented block on line 2. You should fix this by remove the indent.

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

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

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

  • But basically it's, you are using tabs and spaces in your code.
  • You don't want that.
  • Remove all tabs and replaces them by four spaces.
  • And configure your editor to do that automatically.
  • You can get some more info here.
  • 但基本上是这样,您在代码中使用了制表符和空格。
  • 你不想那样。
  • 删除所有选项卡并用四个空格替换它们。
  • 并配置您的编辑器以自动执行此操作。
  • 您可以在此处获得更多信息。



最后,回到你的问题:

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

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

Just look at the line number of the error, and fix it using the previous information.

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