# [Python] elif & else

## Recommended Posts

Everytime I type them it says Invalid Syntax... Help?

##### Share on other sites
Show an example of code that gives that problem. Make sure to use [code] or [source] tags to ensure that the spacing shows up properly.

##### Share on other sites
Did you put colons after them?

ie

if blah:    do_stuff()elif that:    do_other_stuff()else:    do_nothing()

##### Share on other sites
>>> if 9 == 9:	do_stuff()	elif that:		SyntaxError: invalid syntax

Elif is higlighted.

##### Share on other sites
Quote:
 Original post by X Jado X>>> if 9 == 9: do_stuff() elif that: SyntaxError: invalid syntaxElif is higlighted.

The "elif" needs to be indented to the same level as the "if".

##### Share on other sites
Ahh I see, thank you.

##### Share on other sites
Quote:
Original post by bakery2k1
Quote:
 Original post by X Jado X>>> if 9 == 9: do_stuff() elif that: SyntaxError: invalid syntaxElif is higlighted.

The "elif" needs to be indented to the same level as the "if".

Yeah that's one of the things that first aggravated me when I first learned Python never mind not using elseif like 99% of the other languages!
Since whitespace matters also unlike 99% of other languages a good Python editor is essential since I used the one that came with Python and it never alerted me to silly mistakes like this.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628290
• Total Posts
2981858

• 11
• 10
• 10
• 11
• 17