python出现 unindent does not match any outer indentation level、unexpected indent、inconsistent use of..

问题如下:

python肉眼看起来対得整整齐齐的代码为什么还会显示对不齐呢?具体问题像下面:

IndentationError: unindent does not match any outer indentation level

IndentationError: unexpected indent

TabError: inconsistent use of tabs and spaces in indentation

出现这三种错误提示,主要是因为taps和space错误使用导致python代码没有对齐而无法编译。特别是从网上copy下来的代码,你不知道对方对齐时候的制表符是怎么设置的。

解决办法:

法一:

直接复制,比如:问题代码

	c = c.astype("int")
#	text = "{} {}".format(color, shape)
	# 绘制轮廓并显示结果
  	if shape == 1:          
          cv2.drawContours(image, [c], -1, (0, 255, 0), 2)

我直接复制第一行c前面的制表符,解决TabError: inconsistent use of tabs and spaces in indentation。

	c = c.astype("int")
#	text = "{} {}".format(color, shape)
	# 绘制轮廓并显示结果
	if shape == 1:          
          cv2.drawContours(image, [c], -1, (0, 255, 0), 2)

像这种嵌套的语句,问题也可能出现在后面的行,不过这时用tap直接对齐就好了。想看的话复制到doc就可以看见问题了。

法二:显示制表符逐句修改

参考:

https://www.cnblogs.com/heimanba/p/3783022.html

法三:打开sublime的空格制表显示就可以清楚的显示出自己是否真的空格了。

参考:

https://blog.csdn.net/qq_41096996/article/details/85947560

后面两种方法亲测不好用,可以参考一下吧,因为网友们使用的编译器不同,所以显示制表符位置不一样,从原理上讲是可以的。

你可能感兴趣的:(python)