【问题标题】:Why am I getting this error on Python File "<stdin>", line 2 /n new_squares.append(squares[i]) /n ^ IndentationError: expected an indented block?为什么我在 Python 文件“<stdin>”第 2 行 /n new_squares.append(squares[i]) /n ^ IndentationError: expected an indented block 上收到此错误?
【发布时间】:2020-04-24 01:51:21
【问题描述】:

我是 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

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

【问题讨论】:

    标签: python python-2.7


    【解决方案1】:

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

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

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

    然后将以下代码new_squares.append(squares[i])向右缩进一级

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-18
      • 1970-01-01
      • 2021-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多