python基础===使用switch方法,减少使用if语句

def jia(x,y):
    return x+y
def jian(x,y):
    return x-y
def cheng(x,y):
    return x*y
def chu(x,y):
    return x/y

#建一个字典,使得对应的运算符取到对应的运算方法:
operator = {"+":jia,"-":jian,"*":cheng,"/":chu}


def f(x,o,y):
    #单独的看operator.get(o),其实就等于,jia,jian,cheng,chu 中的一种
    print(operator.get(o)(x,y))


f(4,"+",9)

#switch 实现 加减乘除的方法!

 

posted @ 2017-08-15 21:35  botoo  阅读(772)  评论(0编辑  收藏  举报