`
fxly0401
  • 浏览: 146761 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

IndentationError:expected an indented block

阅读更多
Python语言是一款对缩进非常敏感的语言,给很多初学者带来了困惑,即便是很有经验的Python程序员,也可能陷入陷阱当中。最常见的情况是tab和空格的混用会导致错误,或者缩进不对,而这是用肉眼无法分别的。
在编译时会出现这样的错IndentationError:expected an indented block说明此处需要缩进,你只要在出现错误的那一行,按空格或Tab(但不能混用)键缩进就行。
往往有的人会疑问:我根本就没缩进怎么还是错,不对,该缩进的地方就要缩进,不缩进反而会出错,,比如:

if xxxxxx:
(空格)xxxxx

或者

def xxxxxx:

(空格)xxxxx

还有

for xxxxxx:

(空格)xxxxx


一句话 有冒号的下一行往往要缩进,该缩进就缩进

一个.py文件,如果是自身在运行,那么它的__name__值就是"__main__"; 如果它是被别的程序导入的(作为一个模块),比如:import test 那么,他的__name__就不是"__main__"了。 所以,在.py文件中使用这个条件语句,可以使这个条件语句块中的命令只在它独立运行时才执行
分享到:
评论

相关推荐

    python常见错误.txt

    (导致"IndentationError:unexpected indent"、"IndentationError:unindent does not match any outer indetation level"以及"IndentationError:expected an indented block") 记住缩进增加只用在以:

    17个新手常见Python运行时错误.docx

    如果您错误地使用缩进,将会报IndentationError:unexpected indent、IndentationError:unindent does not match any outer indentation level 或 IndentationError:expected an indented block 错误。 4. 在 for...

    Python--学习笔记.docx

    在编译时,如果 Python 解释器遇到缩进错误,会抛出 IndentationError:expected an indented block。解决方法是,在出现错误的那一行,按空格或 Tab 键缩进。 id() 函数 Python 的 id() 函数可以获得对象的内存...

    《Python编程案例教程》第章-异-常.pptx

    例如,如果在for循环之后的代码没有正确缩进,Python会提示"IndentationError: expected an indented block"。 为了处理这些异常,Python提供了异常处理结构,包括try/except、try/except/else和try/finally语句。...

    新手常见Python运行时错误汇总.pdf

    9. IndentationError: unexpected indent、IndentationError: unindent does not match any outer indentation level、IndentationError: expected an indented block 在Python中,缩进需要正确使用。例如: print...

    python小课知识点.docx

    - `IndentationError: expected an indented block`:Python依靠缩进来组织代码块,如果缩进不正确,会导致此错误。 - `KeyError: 'fond'`:在字典中尝试访问不存在的键。 - `ValueError: substring not found`:在...

    17个新手常见Python运行时错误.pdf

    错误的使用缩进量,会导致“IndentationError: unexpected indent”、“IndentationError: unindent does not match any outer indetation level”、“IndentationError: expected an indented block”错误。...

    code-nest:Github 上流行的 javascript 存储库中源代码的缩进级别

    测量流行的 Github 存储库的嵌套级别 ... 在 python 中,缩进是规则而不是指南,任何带有未对齐代码嵌套的 python 脚本都会导致IndentationError: expected an indented block 。 再次在 python 中,如果

    Python代码书写规范与基本使⽤.pdf

    - `IndentationError: expected an indented block`:缩进错误,Python对缩进非常敏感。 - `KeyError: 'fond'`:字典中没有指定的键值。 - `ValueError: substring not found`:值错误,例如在字符串操作中输入的...

    vs没报错leetcode报错-C-Practice:C-实践

    vs没报错leetcode报错C-实践 C++ 1. 2. 3. 4. 5. 6. —— 7. 创建链表 8. 操作员 继承 非虚拟与虚拟 . 纯虚函数,通常在基类中,没有意义,但会在派生类中编译。 虚拟无效 ...动态规划问题,动态规

    新手常见Python运行时错误汇总.docx

    **错误类型**: `IndentationError: unexpected indent`, `IndentationError: unindent does not match any outer indentation level`, `IndentationError: expected an indented block` **原因**: 缩进不正确。 **...

    17个新手常见Python运行时错误

    **错误信息**: `IndentationError: unexpected indent`, `IndentationError: unindent does not match any outer indentation level`, `IndentationError: expected an indented block` **解释**: Python 使用缩...

    python bug清除手册-代码书写规范与基本使用.pdf

    **3.5 IndentationError: expected an indented block** 这种错误发生在代码块的缩进不正确时。Python使用缩进来区分代码块。例如: ```python if True: print("Hello") # 缩进错误 ``` 解决方法:确保所有的代码...

    python中常见错误及解决方法

    这段代码会抛出`IndentationError: expected an indented block`,原因是`if`语句后面的代码行没有正确缩进。Python要求在代码块的开头使用4个空格的缩进。修复这个错误的方法是: ```python a = 1 b = 2 if a < b:...

    Python新手入门最容易犯的错误总结

    上述代码分别会引发 **IndentationError: unexpected indent**、**IndentationError: unindent does not match any outer indentation level** 和 **IndentationError: expected an indented block** 的错误。...

    何寄华投稿1

    2. 错误信息:提供关于错误的简短描述,例如"invalid syntax"或"expected an indented block"等。 3. 出错的文件名和行号:错误信息会指出错误发生在哪个文件的哪一行。 4. 指示器:在错误行的下方,Python会显示一...

Global site tag (gtag.js) - Google Analytics