Invalid syntax on if-else statement
Question:
Can someone explain why I am getting an invalid syntax error from Python’s interpreter while formulating this simple if/else
statement? I don’t add any tabs myself I simply type the text then press enter after typing. When I type an enter after "else:" I get the error. else
is highlighted by the interpreter. What’s wrong?
>>> if 3 > 0:
print("3 greater than 0")
else:
SyntaxError: invalid syntax
Answers:
That’s because your else
part is empty and also not properly indented with the if
.
if 3 > 0:
print "voila"
else:
pass
In python pass
is equivalent to {}
used in other languages like C.
The else
block needs to be at the same indent level as the if
:
if 3 > 0:
print('3 greater then 0')
else:
print('3 less than or equal to 0')
Python does not allow empty blocks, unlike many other languages (since it doesn’t use braces to indicate a block). The pass
keyword must be used any time you want to have an empty block (including in if/else statements and methods).
For example,
if 3 > 0:
print('3 greater then 0')
else:
pass
Or an empty method:
def doNothing():
pass
The keyword else
has to be indented with respect to the if
statement respectively
e.g.
a = 2
if a == 2:
print "a=%d", % a
else:
print "mismatched"
The problem is indent only.
You are using IDLE. When you press enter after first print statement indent of else is same as print by default, which is not OK. You need to go to start of else sentence and press back once. Check in attached image what I mean.
it is a obvious mistake we do, when we press enter after the if statement it will come into that intendation,try by keeping the else statement as straight with the if statement.it is a common typographical error
Else needs to be vertically aligned. Identation is playing a key role in Python. I was getting the same syntax error while using else. Make sure you indent your code properly
Can someone explain why I am getting an invalid syntax error from Python’s interpreter while formulating this simple if/else
statement? I don’t add any tabs myself I simply type the text then press enter after typing. When I type an enter after "else:" I get the error. else
is highlighted by the interpreter. What’s wrong?
>>> if 3 > 0:
print("3 greater than 0")
else:
SyntaxError: invalid syntax
That’s because your else
part is empty and also not properly indented with the if
.
if 3 > 0:
print "voila"
else:
pass
In python pass
is equivalent to {}
used in other languages like C.
The else
block needs to be at the same indent level as the if
:
if 3 > 0:
print('3 greater then 0')
else:
print('3 less than or equal to 0')
Python does not allow empty blocks, unlike many other languages (since it doesn’t use braces to indicate a block). The pass
keyword must be used any time you want to have an empty block (including in if/else statements and methods).
For example,
if 3 > 0:
print('3 greater then 0')
else:
pass
Or an empty method:
def doNothing():
pass
The keyword else
has to be indented with respect to the if
statement respectively
e.g.
a = 2
if a == 2:
print "a=%d", % a
else:
print "mismatched"
The problem is indent only.
You are using IDLE. When you press enter after first print statement indent of else is same as print by default, which is not OK. You need to go to start of else sentence and press back once. Check in attached image what I mean.
it is a obvious mistake we do, when we press enter after the if statement it will come into that intendation,try by keeping the else statement as straight with the if statement.it is a common typographical error
Else needs to be vertically aligned. Identation is playing a key role in Python. I was getting the same syntax error while using else. Make sure you indent your code properly