本文的目的是介绍IndentationError期望缩进的块的详细情况,特别关注期望缩进块什么意思的相关信息。我们将通过专业的研究、有关数据的分析等多种方式,为您呈现一个全面的了解Indentatio
本文的目的是介绍IndentationError期望缩进的块的详细情况,特别关注期望缩进块什么意思的相关信息。我们将通过专业的研究、有关数据的分析等多种方式,为您呈现一个全面的了解IndentationError期望缩进的块的机会,同时也不会遗漏关于ArcMap python IndentationError: unexpected indent 莫名的错误解决、Indentation error codes、IndentationError: unexpected indent python、IndentationError: unindent does not match any oute的知识。
本文目录一览:- IndentationError期望缩进的块(期望缩进块什么意思)
- ArcMap python IndentationError: unexpected indent 莫名的错误解决
- Indentation error codes
- IndentationError: unexpected indent python
- IndentationError: unindent does not match any oute
IndentationError期望缩进的块(期望缩进块什么意思)
这是代码:
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 = Trueelse: result = Falsereturn result
这是错误:
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
我的代码有什么问题?
答案1
小编典典您混用了制表符和空格。这可能会导致一些令人困惑的错误。
我建议使用 唯一的 标签或 仅 缩进空间。
通常只使用空格是较容易的选择。大多数编辑器都有一个选项,可以将制表符自动转换为空格。如果您的编辑器具有此选项,请将其打开。
顺便说一句,您的代码比需要的更为冗长。代替这个:
if str_p == str_q: result = Trueelse: result = Falsereturn result
只是这样做:
return str_p == str_q
您在此行上似乎也有一个错误:
str_q = p[b+1:]
我将让您找出错误所在。
ArcMap python IndentationError: unexpected indent 莫名的错误解决
ArcMap python IndentationError: unexpected indent 莫名的错误解决,太烦恼了,搞了好久,搞定
关注我的微信公众号
打开源代码,保存为ansi格式,如图
Indentation error codes
code | sample message |
---|---|
E1 | Indentation |
E101 | indentation contains mixed spaces and tabs |
E111 | indentation is not a multiple of four |
E112 | expected an indented block |
E113 | unexpected indentation |
E114 | indentation is not a multiple of four (comment) |
E115 | expected an indented block (comment) |
E116 | unexpected indentation (comment) |
E121 (*^) | continuation line under-indented for hanging indent |
E122 (^) | continuation line missing indentation or outdented |
E123 (*) | closing bracket does not match indentation of opening bracket’s line |
E124 (^) | closing bracket does not match visual indentation |
E125 (^) | continuation line with same indent as next logical line |
E126 (*^) | continuation line over-indented for hanging indent |
E127 (^) | continuation line over-indented for visual indent |
E128 (^) | continuation line under-indented for visual indent |
E129 (^) | visually indented line with same indent as next logical line |
E131 (^) | continuation line unaligned for hanging indent |
E133 (*) | closing bracket is missing indentation |
E2 | Whitespace |
E201 | whitespace after ‘(‘ |
E202 | whitespace before ‘)’ |
E203 | whitespace before ‘:’ |
E211 | whitespace before ‘(‘ |
E221 | multiple spaces before operator |
E222 | multiple spaces after operator |
E223 | tab before operator |
E224 | tab after operator |
E225 | missing whitespace around operator |
E226 (*) | missing whitespace around arithmetic operator |
E227 | missing whitespace around bitwise or shift operator |
E228 | missing whitespace around modulo operator |
E231 | missing whitespace after ‘,’, ‘;’, or ‘:’ |
E241 (*) | multiple spaces after ‘,’ |
E242 (*) | tab after ‘,’ |
E251 | unexpected spaces around keyword / parameter equals |
E261 | at least two spaces before inline comment |
E262 | inline comment should start with ‘# ‘ |
E265 | block comment should start with ‘# ‘ |
E266 | too many leading ‘#’ for block comment |
E271 | multiple spaces after keyword |
E272 | multiple spaces before keyword |
E273 | tab after keyword |
E274 | tab before keyword |
E275 | missing whitespace after keyword |
E3 | Blank line |
E301 | expected 1 blank line, found 0 |
E302 | expected 2 blank lines, found 0 |
E303 | too many blank lines (3) |
E304 | blank lines found after function decorator |
E305 | expected 2 blank lines after end of function or class |
E4 | Import |
E401 | multiple imports on one line |
E402 | module level import not at top of file |
E5 | Line length |
E501 (^) | line too long (82 > 79 characters) |
E502 | the backslash is redundant between brackets |
E7 | Statement |
E701 | multiple statements on one line (colon) |
E702 | multiple statements on one line (semicolon) |
E703 | statement ends with a semicolon |
E704 (*) | multiple statements on one line (def) |
E711 (^) | comparison to None should be ‘if cond is None:’ |
E712 (^) | comparison to True should be ‘if cond is True:’ or ‘if cond:’ |
E713 | test for membership should be ‘not in’ |
E714 | test for object identity should be ‘is not’ |
E721 (^) | do not compare types, use ‘isinstance()’ |
E731 | do not assign a lambda expression, use a def |
E741 | do not use variables named ‘l’, ‘O’, or ‘I’ |
E742 | do not define classes named ‘l’, ‘O’, or ‘I’ |
E743 | do not define functions named ‘l’, ‘O’, or ‘I’ |
E9 | Runtime |
E901 | SyntaxError or IndentationError |
E902 | IOError |
W1 | Indentation warning |
W191 | indentation contains tabs |
W2 | Whitespace warning |
W291 | trailing whitespace |
W292 | no newline at end of file |
W293 | blank line contains whitespace |
W3 | Blank line warning |
W391 | blank line at end of file |
W5 | Line break warning |
W503 (*) | line break occurred before a binary operator |
W6 | Deprecation warning |
W601 | .has_key() is deprecated, use ‘in’ |
W602 | deprecated form of raising exception |
W603 | ‘<>’ is deprecated, use ‘!=’ |
W604 | backticks are deprecated, use ‘repr()’ |
打开:File - Settings…… - Editor - Inspections
在python下找到 PEP8 coding style violation,在右边下面的Ignore errors里可以添加忽略的警告信息ID
IndentationError: unexpected indent python
都知道python是对格式要求很严格的,写了一些python但是也没发现他严格在哪里,今天遇到了IndentationError: unexpected indent错误我才知道他是多么的严格。
以后遇到了IndentationError: unexpected indent你就要知道python编译器是在告诉你“Hi,老兄,你的文件里格式不对了,可能是tab和空格没对齐的问题,你需要检查下tab和空格了”。
在windows上你可以用editplus看的,虽然我不知道怎么显示出tab,但是他能显示你的文件是没对齐的,我是在ubuntu上编写python的。我用的是vim,你可以设置下vim中tab用空格替换,我在前一篇文章里写过关于这个设置的问题,我现在在补充一下,之前是在系统目录下设置的,现在我要在home目录下设置;
首先 cd ~(到你的home目录)
然后 ls -a(显示隐藏的文件)
找到.vimrc(记得是带个点前缀的)
然后编辑这个文件
找到如下的命令:
16 set autoindent
17 set smartindent
19 set tabstop=4
20 set shiftwidth=4
21 set softtabstop=4
22 set noexpandtab
然后在set tabstop=4前面加上set expandtab同时你需要把set noexpandtab这个注释掉,怎么注释呢,就是加个双引号。我没试过直接注释不加set expandtab,我想也可以(难道默认是expandtab)。这样你可以放心使用VIM了。
当然我并不是想就这样完事,我其实想看下我的代码到底哪里会有问题呢,就需要看下他的格式了,主要是看tab,这个可以同样在刚才那个文件里设置,在刚才设置的下面比如说
在set softtabstop=4下面加上一句set list。这句就可以让你的vim打开的文件显示出tab了,当然也许你觉得显示的不好看,他显示的样子是“^I”一个数字键6上那个符号和一个大写的I表示一个tab,这个不会显示出空格。他还会显示出段落的起始符^和终止符$,如果不知道这2个符号可以看下正则表达式。
以上2点设置以后,我想以后你就不会遇到IndentationError: unexpected indent这个很烦人的错误了。
以上的set命令都可以单独使用,但是只对当前打开的vim有效,你可以按下shift加冒号,然后输入set XXX,回车然后就有效果了,但是你要再次进入编辑模式才能看到。
希望分享给每个写python的人。
IndentationError: unindent does not match any oute
【问题】
一个python脚本,本来都运行好好的,然后写了几行代码,而且也都确保每行都对齐了,但是运行的时候,却出现语法错误:
IndentationError: unindent does not match any outer indentation level
【解决过程】
1.对于此错误,最常见的原因是,的确没有对齐。但是我根据错误提示的行数,去代码中看了下,没啥问题啊。
都是用TAB键,对齐好了的,没有不对齐的行数啊。
2.以为是前面的注释的内容影响后面的语句的语法了,所以把前面的注释也删除了。
结果还是此语法错误。
3.后来折腾了半天,突然想到了,把当前python脚本的所有字符都显示出来看看有没有啥特殊的字符。
当前用的文本编辑器Notepad++,好像有个设置,可以显示所有的字符的。
找到了,在:
视图 -> 显示符号 -> 显示空格与制表符
然后就看出问题来了:
原来错误的行数是1580行:
但是源码的1580行的对齐用的是点点点的空格,是和前面的几行的对齐所用的箭头表示的TAB键,是不匹配的,即代码的对齐,混用了TAB键和空格:
而新的Python语法,是不支持的代码对齐中,混用TAB和空格的。所以出现上述错误提示了。
知道原因了,解决起来就简单了:
去把对应的TAB,都改为空格,统一一下对齐的风格,即可。
在Notepad++中,去:
设置->首选项:
语言->以空格取代(TAB键):
即可实现,对于以后每次的TAB输入,都自动转换为4个空格。
【总结】
Python中遇到IndentationError,以后第一时间就要想到,是不是由于TAB键和空格混搭使用了。
估计很多人也都是此类原因导致的。记得统一一下就好。
捐赠(Donate): |
关于IndentationError期望缩进的块和期望缩进块什么意思的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于ArcMap python IndentationError: unexpected indent 莫名的错误解决、Indentation error codes、IndentationError: unexpected indent python、IndentationError: unindent does not match any oute的相关信息,请在本站寻找。
本文标签: