IndentationError:expected an indented block

IndentationError:expected an indented block

目录

[IndentationError:expected an indented block](#IndentationError:expected an indented block)

【常见模块错误】

【解决方案】


欢迎来到我的主页,我是博主英杰,211科班出身,就职于医疗科技公司,热衷分享知识,武汉城市开发者社区主理人

擅长.net、C++、python开发, 如果遇到技术问题,即可私聊博主,博主一对一为您解答

修改代码、商务合作:

Yan--yingjie

Yan--yingjie

Yan--yingjie

【常见模块错误】

如果出现模块错误

python 复制代码
进入控制台输入:建议使用国内镜像源

pip install 模块名称 -i https://mirrors.aliyun.com/pypi/simple

我大致罗列了以下几种国内镜像源:

清华大学
https://pypi.tuna.tsinghua.edu.cn/simple
     
阿里云
https://mirrors.aliyun.com/pypi/simple/
     
豆瓣
https://pypi.douban.com/simple/
     
百度云
https://mirror.baidu.com/pypi/simple/
     
中科大
https://pypi.mirrors.ustc.edu.cn/simple/
     
华为云
https://mirrors.huaweicloud.com/repository/pypi/simple/
     
腾讯云
https://mirrors.cloud.tencent.com/pypi/simple/

【解决方案】

在Python编程中,IndentationError: expected an indented block错误通常是因为代码块的缩进不正确导致的。Python使用缩进来定义控制结构(如循环、条件语句和函数定义)的范围。如果在需要缩进的语句后没有提供预期的缩进,就会出现这个错误。

解决这个错误的方法包括:

  1. 检查缩进是否一致:确保所有代码块的第一行都有相同的缩进级别。Python推荐使用四个空格作为标准缩进,而不是混用制表符和空格。

  2. 避免混用制表符和空格:混合使用制表符(Tab)和空格会导致缩进错误。建议使用文本编辑器的缩进功能来自动处理缩进问题。

  3. 检查代码块的开头和结尾:确保在if、for、while等语句之后有正确的缩进块。例如,在一个if语句之后,应该有一个缩进的代码块,如:

    复制代码
    if condition:

    indented block

如果缺少或缩进不正确,就会引发IndentationError

编译时会出现这样的错IndentationError:expected an indented block说明此处需要缩进,你只要在出现错误的那一行,按空格或Tab键缩进。

该缩进的地方就要缩进,不缩进反而会出错,,比如:

if xxxxxx:

(空格)xxxxx

或者

def xxxxxx:

(空格)xxxxx

还有

for xxxxxx:

(空格)xxxxx

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

相关推荐
没有韭菜的饺子8 小时前
记录一个IDEA的神奇bug
bug
许愿OvO8 小时前
MySQL触发器
android·mysql·adb
lcanfly8 小时前
Mysql作业4
数据库·mysql
Q_Q19632884758 小时前
python+django/flask基于协同过滤算法的理财产品推荐系统
spring boot·python·django·flask·node.js·php
高洁018 小时前
面向强化学习的状态空间建模:RSSM的介绍和PyTorch实现(3)
人工智能·python·深度学习·神经网络·transformer
aloha_7899 小时前
测试开发工程师面经准备(sxf)
java·python·leetcode·压力测试
蓝象_9 小时前
docker安装配置mysql
mysql·docker·容器
喜欢踢足球的老罗9 小时前
认证与授权:详解大型系统中用户中心与RBAC的共生关系
数据库·rbac
zhaomx19899 小时前
Spring 事务管理 Transaction rolled back because it has been marked as rollback-only
数据库·spring