簡體   English   中英

為什么我在 Python 文件上收到此錯誤“<stdin> ”,第 2 行 /n new_squares.append(squares[i]) /n ^ IndentationError: 期望縮進塊?</stdin>

[英]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 的新手,我剛剛了解了“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])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM