python基础===使用switch方法,减少使用if语句
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 实现 加减乘除的方法!
以上是关于python基础===使用switch方法,减少使用if语句的主要内容,如果未能解决你的问题,请参考以下文章