Majority of the time new Python developers face one common problem and that is of IndentationError: unindent does not match any outer indentation level and they are not able to figure out why this error occurred.

大多数时候,新的Python开发人员都面临一个常见的问题,那就是IndentationError:unindent与任何外部缩进级别都不匹配,并且他们无法弄清楚为什么会发生此错误。

In this post, I’ll be discussing some of the ways to overcome IndentationError. But before getting started, I’d like to tell you guys that Python is very strict about Indentation and if any of your code blocks is not indented, the interpreter is going to complain about the indentation.

在本文中,我将讨论一些克服IndentationError的方法。 但是在开始之前,我想告诉大家Python对缩进非常严格,如果您的任何代码块都未缩进,则解释器将抱怨缩进。

Let us see this with an example.

让我们看一个例子。

print("Hello, World!")

Here, you can see that the code has an extra space in front of it and when you’ll try to run your python file, this will throw an error.

在这里,您可以看到代码前面有一个额外的空间,当您尝试运行python文件时,这将引发错误。

如何解决错误? (How to Solve the Error?)

停止使用空格 (Stop Using spaces)

The best way to avoid these kinds of error is to stop using spaces for indentation and start using tabs for indentation.

避免此类错误的最佳方法是停止使用空格进行缩进,而开始使用制表符进行缩进。

When you mix spaces with tabs in your code, then the compiler throws an indentation error.

当您在代码中将空格与制表符混合时,编译器将引发缩进错误。

标签的使用 (Use of Tabs)

Use tabs in place of spaces and this will remove all the indentation error your python code may have. This maintains consistency which reduces the chance of indentation error in your code. Using spaces may lead to some extra space but tabs are of fixed width. It will always give you either 2 space or 4 space depending upon your editor’s configuration.

使用制表符代替空格,这将消除您的python代码可能存在的所有缩进错误。 这样可以保持一致性,从而减少代码中出现缩进错误的可能性。 使用空格可能会导致一些额外的空间,但是制表符的宽度是固定的。 根据编辑器的配置,它将始终为您提供2个空间或4个空间。

使用代码格式化程序 (Using Code Formatter)

One another way to get rid of indentation error is to use some code formatter.  This will help you to format your code properly and will help you to format code blocks which are not properly formatted.

消除缩进错误的另一种方法是使用某些代码格式化程序。 这将帮助您正确设置代码格式,并帮助您格式化未正确设置格式的代码块。

I hope this will solve the error. Still if you have any queries ask in the comment section.

我希望这可以解决错误。 不过,如果您有任何疑问,请在评论部分中询问。

翻译自: https://www.thecrazyprogrammer.com/2020/08/unindent-does-not-match-any-outer-indentation-level.html

解决IndentationError:unindent与任何外部缩进级别都不匹配相关推荐

  1. python整体向右缩进两个级别_关于python:IndentationError:unindent与任何外部缩进级别都不匹配...

    当我编译下面的python代码时,我得到 IndentationError: unindent does not match any outer indentation level import sy ...

  2. [bug解决] IndentationError unindent does not match any outer indentation level

    文章目录 问题描述: 产生原因: 解决方案: 问题描述: 运行Python代码,出现如下报错: IndentationError: unindent does not match any outer ...

  3. IndentationError: unindent does not match any outer indentation level 解决办法

    执行Python事报错: IndentationError: unindent does not match any outer indentation level 译文: 缩进错误:未缩进与任何外部 ...

  4. IndentationError: unindent does not match any outer indentation level-报错问题

    在python中遇到这样的问题 翻译过来是: 缩进错误:未缩进与任何外部缩进级别都不匹配 很显然,一般都是格式没对,你看看你的代码对齐了没有.python对缩进非常的严格呢! 例如: 此处我的j多打了 ...

  5. 关于“IndentationError: unindent does not match any outer indentation level”的解决方法

    1.问题 在cmd窗口运行python脚本,运行失败,提示"IndentationError: unindent does not match any outer indentation l ...

  6. 解决python中出现IndentationError:unindent does not match any outer indentation level错误

    运行Python程序时,出现"IndentationError:unindent does not match any outer indentation level"错误提示,检 ...

  7. 解决:IndentationError: unindent does not match any outer indentation level

    问题: 执行python脚本时,提示如下错误信息: IndentationError: unindent does not match any outer indentation level 问题排查 ...

  8. Python脚本运行出现语法错误:IndentationError: unindent does not match any outer indentation level...

    http://www.crifan.com/python_syntax_error_indentationerror/comment-page-1/ [问题]  一个python脚本,本来都运行好好的 ...

  9. Python 脚本错误:IndentationError: unindent does not match any outer indentation level

    好好的Python脚本,确定了每行都已经正确对齐了, 可是运行时还是会遇到提示缩进有误的问题:IndentationError: unindent does not match any outer i ...

  10. Python脚本运行出现语法错误:IndentationError: unindent does not match any outer indentation level

    今天调试python遇到IndentationError: unindent does not match any outer indentation level,错误看到一个不错的帖子转发下 [问题 ...

最新文章

  1. Django 视图URLconf3.1
  2. 推荐2个命令快速在本地和服务器之间上传下载文件
  3. 京东java社招面试6_2020最新京东java面试题分享,京东社招
  4. 【渝粤教育】电大中专中成药作业 题库
  5. 期权回测框架设计思路
  6. Aruba 推出Instant On 为中小型企业提供安全、高速的无线连接
  7. php字符串中删除字符串函数,PHP实现删除字符串中任何字符的函数
  8. 邵阳职院计算机专业怎么样,邵阳市古峰职业学校怎么样?
  9. box-shadow单边阴影设置
  10. golang的ping检测主机存活
  11. a += a-= a*a
  12. 郭德纲的网络效应和网络利用
  13. 运用程序化交易系统的能力表现在哪些方面?
  14. matlab中画网格,matlab怎么画网格
  15. 腾讯企业邮箱登录、网易企业邮箱、TOM企业邮箱,各大企业邮箱如何申请登录?
  16. stm32/esp32/cam智能安防系统远程视频监控物联网远程传输视频图像GSM短信报警
  17. Andersen Global拓展巴西业务版图;在巴西新增合作公司
  18. 43 岁硅谷技术大拿命丧街头,真凶落网:熟人作案,是 Expand IT 创始人
  19. C语言中的随机数生成器
  20. 做好flash手绘基本功,简单的手绘人物头像和眨眼动画

热门文章

  1. navicat mysql密码重置_navicat如何重置密码
  2. 主持人大赛-即兴主持之新闻稿
  3. svn多分支开发合并技巧(idea or tortoiseSVN)
  4. DHCP中继配置(思科)
  5. vm镜像 黑群晖_使用Vmware Workstation安装黑群晖
  6. 边缘计算是什么,对企业有什么影响?
  7. CentOS 7.9命令行配置有线网卡
  8. C++ 依赖倒置原则
  9. 分类模型的再考以及随机森林的应用
  10. Android-adb获取当前前台进程