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

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

posted @   狂小虎  阅读(86)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示