如果和elif声明

时间:2015-05-20 09:58:48

标签: python python-3.x

如何运行如下,给出if和else语句?

我等待肯定回复

def run():
    a=10
    b=20
    c=30
    if a==15 & b==20 & c==30:
        print('a is diff')
    else a==10 & b==20 & c==30:
        print ('same')
    else a==15 &  b==40 & c==30:
        print('a is diff')

3 个答案:

答案 0 :(得分:1)

您需要elif,而不能使用两个else
此外,else一无所获。 你应该替换& bij and

def run():
    a = 10 
    b = 20 
    c = 30 
    if (a == 15) and (b == 20) and (c == 30): 
        print('a is diff') 
    elif (a == 10) and (b == 20) and (c == 30): 
        print ('same') 
    elif (a == 15) and (b == 40) and (c == 30): 
        print('a is diff')
    else:
        print('Other')

答案 1 :(得分:0)

您应该使用elif代替else

答案 2 :(得分:0)

我已经更正了您的代码,假设您希望代码示例返回“相同”。我已将else替换为elif,将按位运算符&替换为and

def run(): 
    a=10 
    b=20 
    c=30 
    if a==15 and b==20 and c==30: 
        print('a is diff') 
    elif a==10 and b==20 and c==30: 
        print ('same') 
    elif a==15 and b==40 and c==30: 
       print('a is diff')

run()