python - 为什么我在 Python 文件上收到此错误“",第 2 行 /n new_squares.append(squares[i]) /n ^ IndentationError: 期望缩进块?
问题描述
我是 python 新手,我刚刚了解了“While Loops”所以我正在尝试这段代码,它包括将列表的值复制到另一个列表中
>>> squares=['red','red','red','blue','red','red']
>>> new_squares=[];
>>> i=0
>>> while(squares[i]=='red')
... new_squares.append(squares[i])
现在是发生错误的时候
File "<stdin>", line 2
new_squares.append(squares[i])
^
IndentationError: expected an indented block
为什么会发生这种情况,我在课程中使用了相同的代码,我只是将值从“橙色”更改为“红色”
解决方案
语句应以while
结尾:
,因此您应该更改
while(squares[i]=='red')
至
while(squares[i]=='red'):
然后new_squares.append(squares[i])
在右侧的一级缩进处编写以下代码
推荐阅读
- couchdb - 如何制作具有离线和在线(同步数据)能力的电子应用程序?
- mysql - 对值不为空的查询结果进行分组
- javascript - 如何更改react-table中单行的边框颜色
- asp.net-core - 如何使用核心依赖注入按环境指定 NLog 的配置?
- ios - 如何在 iPad OS 中从 Hockeyapp(或类似软件)下载企业签名应用程序?
- python - Firestore(python)侦听器完成脚本而不是侦听更改
- twig - 如何在表单标签中创建与 Twig 的链接
- java - 如何在 java 中使我的 JSON 更好?严格使用 - org.json.JSON
- office365 - Impersonator 是否需要访问整个租户才能连接
- mysql-workbench - 在 127.0.0.1:3306 上添加客户端到服务器端口转发规则失败