How could I use more commands in the if/else statement in Python?

Question:

I wanted to apply more than 1 command under the if/else statement in a simple Python program.
Python has the following error message: IndentationError: unindent does not match any outer indentation level.
I used VSCode to apply this program.

How could I use more commands in the same if/else statement?
I tried to run the following program, but it did not work properly, because of identation problem:

colourlist = ['blue', 'red', 'white', 'black', 'green', 'yellow', 'brown', 'red', 'white', 'grey']

colour = input('Give me a colour!t')

if colour in colourlist:
    print('The colour you have given is in the list'+ str(colourlist.count(colour)) +' time/s.')
else:
    colourlist.append(colour)
        print('The given colour is not in the list.')
print('The colours of the list:')
for colour in colourlist:
    print(colour, end=', ')
Asked By: Grindelwald

||

Answers:

Your line number 9 is indented with 8 spaces, that’s not necessary since you don’t have a new block of code.

To run new commands, just remove the unnecessary blank spaces from that line and it should work just fine. Your code should look something like this

colourlist = ['blue', 'red', 'white', 'black', 'green', 'yellow', 'brown', 'red', 'white', 'grey']

colour = input('Give me a colour!t')

if colour in colourlist:
    print('The colour you have given is in the list'+ str(colourlist.count(colour)) +' time/s.')
else:
    colourlist.append(colour)
        # print('The given colour is not in the list.') -- OLD PRINT STATEMENT
    print('The given colour is not in the list.')
        
print('The colours of the list:')
for colour in colourlist:
    print(colour, end=', ')

Python cares about your code indentation, so just indent your code when you have a new block of code, such as: while, for, if

To use more commands in the if/else statement you just need to write your code respecting the if/else block indentation

Answered By: João Amgarten
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.