blueuser blueuser - 1 year ago 65
Python Question

Invalid syntax on VERY SIMPLE Python if ... else statement

Can someone explain why I am getting an invalid syntax error from Python's interpretor 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?

Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48)
[MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.

>>> if 3 > 0:
print("3 greater than 0")

SyntaxError: invalid syntax


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')

Or an empty method:

def doNothing():