简体   繁体   English

IndentationError 需要一个缩进块

[英]IndentationError expected an indented block

Here is the code:这是代码:

def myfirst_yoursecond(p,q):

a = p.find(" ")
b = q.find(" ")
str_p = p[0:a]
str_q = p[b+1:]

if str_p == str_q:
    result = True
else:
    result = False
return result

Here is the error:这是错误:

Traceback (most recent call last):
  File "vm_main.py", line 26, in <module>
    import main
  File "/tmp/vmuser_ssgopfskde/main.py", line 22
    result = False
         ^
IndentationError: expected an indented block

What's wrong with my code?我的代码有什么问题?

You've mixed tabs and spaces.你混合了制表符和空格。 This can lead to some confusing errors.这可能会导致一些令人困惑的错误。

I'd suggest using only tabs or only spaces for indentation.我建议使用制表符或仅使用空格进行缩进。

Using only spaces is generally the easier choice.仅使用空格通常是更容易的选择。 Most editors have an option for automatically converting tabs to spaces.大多数编辑器都有自动将制表符转换为空格的选项。 If your editor has this option, turn it on.如果您的编辑器有此选项,请将其打开。


As an aside, your code is more verbose than it needs to be.顺便说一句,您的代码比它需要的更冗长。 Instead of this:取而代之的是:

if str_p == str_q:
    result = True
else:
    result = False
return result

Just do this:只需这样做:

return str_p == str_q

You also appear to have a bug on this line:您似乎在这一行也有一个错误:

str_q = p[b+1:]

I'll leave you to figure out what the error is.我会让你弄清楚错误是什么。

This error also occurs if you have a block with no statements in it如果块中没有语句,也会发生此错误

For example:例如:

def my_function():
    for i in range(1,10):


def say_hello():
    return "hello"

Notice that the for block is empty.请注意for块是空的。 You can use the pass statement if you want to test the remaining code in the module.如果要测试模块中的其余代码,可以使用pass语句。

If you are using a mac and sublime text 3, this is what you do.如果您使用的是 mac 和 sublime text 3,这就是您要做的。

Go to your /Packages/User/ and create a file called Python.sublime-settings .转到您的/Packages/User/并创建一个名为Python.sublime-settings的文件。

Typically /Packages/User is inside your ~/Library/Application Support/Sublime Text 3/Packages/User/Python.sublime-settings if you are using mac os x.如果您使用的是 mac os x,通常/Packages/User位于您的~/Library/Application Support/Sublime Text 3/Packages/User/Python.sublime-settings

Then you put this in the Python.sublime-settings .然后你把它放在Python.sublime-settings

{
    "tab_size": 4,
    "translate_tabs_to_spaces": false
}

Credit goes to Mark Byer's answer , sublime text 3 docs and python style guide .归功于 Mark Byer 的回答sublime text 3 docspython style guide

This answer is mostly for readers who had the same issue and stumble upon this and are using sublime text 3 on Mac OS X.该答案主要适用于遇到相同问题并偶然发现并在 Mac OS X 上使用 sublime text 3 的读者。

I got the same error, This is what i did to solve the issue.我遇到了同样的错误,这就是我为解决问题所做的。

Before Indentation:缩进前:

在此处输入图片说明

Indentation Error: expected an indented block.缩进错误:需要一个缩进的块。

After Indentation:缩进后:

在此处输入图片说明

Working fine.工作正常。 After TAB space.在 TAB 空格之后。

You should install a editor (or IDE) supporting Python syntax.您应该安装支持 Python 语法的编辑器(或 IDE)。 It can highlight source code and make basic format checking.它可以突出显示源代码并进行基本的格式检查。 For example: Eric4, Spyder, Ninjia, or Emacs, Vi.例如:Eric4、Spyder、Ninjia 或 Emacs、Vi。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM