python 中分支结构(switch)

时间:2022-04-12 16:56:57

可通过字典调用:{1:case1,2:case2}.get(x,lambda *args,**key:)()

# 编写一个计算器 # -*- coding=utf-8 -*-

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) # def panduan(x,i,y): # if i=="+": #  jia(x,y) # elif i=='-': #  jian(x,y) # elif i=='*': #  cheng(x,y) # elif i=='/': #  chu(x,y) # # else: #  pass panduan={'+':jia,'-':jian,'*':cheng,'/':chu}

def f(x,i,y):

  return (panduan.get(i)(x,y))  #(x,y,*args,**kwargs))

e=f(34,'/',2)

print (e)