views:

678

answers:

6

I'm a new Python programmer who is making the leap from 2.6.4 to 3.1.1. Everything has gone fine until I tried to use the 'else if' statement. The interpreter gives me a syntax error after the 'if' in 'else if' for a reason I can't seem to figure out.

def function(a):
    if a == '1':
        print ('1a')
    else if a == '2'
        print ('2a')
    else print ('3a')

function(input('input:'))

I'm probably missing something very simple; however, I haven't been able to find the answer on my own.

+12  A: 

In python "else if" is spelled "elif".

Also, you need a colon after the elif and the else.

Simple answer to a simple question.

I had the same problem, when I first started (in the last couple of weeks)

So your code should read:

def function(a):
    if a == '1':
        print ('1a')
    elif a == '2':
        print ('2a')
    else:
        print ('3a')

function(input('input:'))
Oxinabox
Also don't forget to put a ":" (colon) at the end of your elif statements.
Tom
Wow, facepalm.... Thanks for the awnser.
Protean
no worries, we all have to learn sometime.I find it weird that python places such an elphisise on readbility and then goes and use elkif instead of else it.I suggest keeping the python API manual open at all times: http://docs.python.org/3.1/the important links areTutorial: http://docs.python.org/3.1/tutorial/index.htmlLanguage reference: http://docs.python.org/3.1/reference/index.htmlLibrary refernce: http://docs.python.org/3.1/library/index.html
Oxinabox
+1  A: 

It looks like your missing some colons on the 'else if' and 'else' statements.

Nick
+2  A: 

Do you mean elif?

Nick Presta
A: 
def function(a):
    if a == '1':
        print ('1a')
    elif a == '2':
        print ('2a')
    else:
        print ('3a')
Tom
A: 

since olden times, the correct syntax for if/else if in Python is elif. By the way, you can use dictionary if you have alot of if/else.eg

d={"1":"1a","2":"2a"}
if not a in d: print("3a")
else: print (d[a])

For msw, example of executing functions using dictionary.

def print_one(arg=None):
    print "one"

def print_two(num):
    print "two %s" % num

execfunctions = { 1 : (print_one, ['**arg'] ) , 2 : (print_two , ['**arg'] )}
try:
    execfunctions[1][0]()
except KeyError,e:
    print "Invalid option: ",e

try:
    execfunctions[2][0]("test")
except KeyError,e:
    print "Invalid option: ",e
else:
    sys.exit()
ghostdog74
You can, but please do not do this. A dictionary is not a good replacement for an `elif`.
S.Lott
@s.lott, OP's case is simple. If he has to check for many values of a, a dictionary is neater. you might make it a habit not to use it, but i have been using it and i like this approach better than coding many if/else. heck, i even use dictionary to execute functions.
ghostdog74
@ghostdog: I know that you *can* use dictionaries to execute functions but the idea scares me like computed gotos or pasting Tcl strings together and `exec`ing them. Is this good practice? Can you name an example?
msw
@msw: It's very good practice. Example: you are reading an XML stream not for some simple scraping exercise but one where you need to do different processing for different element tags e.g. an Excel 2007 spreadsheet file is a zip of multiple XML documents, some very complex. You have a separate method for each tag. You dispatch via a dictionary. Nothing to be scared of. If the method for handling `<foo>` is `do_foo`, you can even build the dict on the fly when the app starts up.
John Machin
understood, thanks much.
msw
+1  A: 

Here is a little refactoring of your function (it does not use "else" or "elif"):

def function(a):
    if a not in (1, 2):
        a = 3
    print(str(a) + "a")

@ghostdog74: Python3.1.1 requires parentheses for "print".

Winston C. Yang
python 3 replaced python 2's print statement with a function thus the required parentheses, and if you've going to so that you might as well just use sys.stdout.write
Dan D