‘else if’的正确语法是什么?

问题:’else if’的正确语法是什么?

我是一名新的Python程序员,他正在从2.6.4跃升至3.1.1。在尝试使用“ else if”语句之前,一切都进行得很好。解释器在“ else if”中的“ if”之后给了我一个语法错误,原因是我似乎无法弄清。

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

function(input('input:'))

我可能缺少一些非常简单的东西;但是,我无法自行找到答案。

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.


回答 0

在python中,“ else if”被拼写为“ elif”。
另外,您还需要在elif和之后加上一个冒号else

简单回答一个简单的问题。刚开始时(过去几周),我遇到了同样的问题。

因此,您的代码应为:

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

function(input('input:'))

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

回答 1

你是说elif


回答 2

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

回答 3

自古以来,if/else ifPython中正确的语法是elif。顺便说一句,如果您有很多if/else.eg ,则可以使用字典。

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

对于msw,使用字典执行函数的示例。

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

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

回答 4

这是函数的一点重构(它不使用“ else”或“ elif”):

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

@ ghostdog74:Python 3要求在“打印”中加上括号。

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: Python 3 requires parentheses for “print”.


回答 5

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

应该更正为:

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

正如您所看到的,else如果应该更改为elif,则’2’之后应该有冒号,否则,else语句之后应该有一个新行,并关闭print和括号之间的空间。

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

Should be corrected to:

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

As you can see, else if should be changed to elif, there should be colons after ‘2’ and else, there should be a new line after the else statement, and close the space between print and the parentheses.