简体   繁体   中英

Why am I getting this error on Python File “<stdin>”, line 2 /n new_squares.append(squares[i]) /n ^ IndentationError: expected an indented block?

I'm new to python, and I just learned about "While Loops" So I'm trying this code which consists on copying the value of a list into another list

>>> squares=['red','red','red','blue','red','red']
>>> new_squares=[];
>>> i=0
>>> while(squares[i]=='red')
... new_squares.append(squares[i])

And now is when the error occurs

  File "<stdin>", line 2
    new_squares.append(squares[i])
              ^
IndentationError: expected an indented block

Why is this happening, I used the same code they do in the course, I just changed the value from 'orange' to 'red'

A while statement should end with : , thus you should change

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

to

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

and then write the following code new_squares.append(squares[i]) at one level of indentation to the right

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM