sarkar.shukla
sarkar.shukla

Reputation: 77

Giving error invalid syntax?

>>> a=1
>>> if a>6:
print "This is my insanity"
else:

SyntaxError: invalid syntax

>

I have written simple if condition to check if condition, but its giving error "invalid syntax"

Upvotes: 0

Views: 498

Answers (4)

Levon
Levon

Reputation: 143047

When I do this I get an IndentationError: expected an indented block, not SyntaxError: invalid syntax. Which Python shell are you using?

The code needs to be properly indented below the if (like it would under a def etc).

Also, else: requires something after it ... for now you could use pass as a place-holder (though if you have nothing to put after the else:, just leave it out)

e.g.

a = 1
if a > 6:
   print "This is my insanity"
else:
   pass  # for now

Upvotes: 3

shiva
shiva

Reputation: 2770

a=1
if a>6:
    print "This is my insanity"
else:
    pass

you should not leave else as empty use pass to do nothing and there is also indentation issue(may be while posting)

Upvotes: -1

axiomer
axiomer

Reputation: 2126

Python needs proper indentation.

a=1
if a>6:
    print "This is my insanity"
else:
    print 'You need to have some command here as well!' # maybe try pass ?

Upvotes: 0

Hunter McMillen
Hunter McMillen

Reputation: 61512

You can't leave the else block empty in your if statement:

>>> a = 1
>>> if a > 6:
...     print "This is my insanity"
... else:
...     print "In else block"
...
In else block

Upvotes: 2

Related Questions