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 实现 加减乘除的方法!
纸上得来终觉浅,绝知此事要躬行!