,python,python-2.7"/>

首页 > 解决方案 > 为什么我在 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

为什么会发生这种情况,我在课程中使用了相同的代码,我只是将值从“橙色”更改为“红色”

标签: pythonpython-2.7

解决方案


语句应以while结尾:,因此您应该更改

while(squares[i]=='red')

while(squares[i]=='red'):

然后new_squares.append(squares[i])在右侧的一级缩进处编写以下代码


推荐阅读