# python是没有专门的switch关键字的,但是有灵活替代方案。 # 方式一: a = 2 b = 3 result = { '+' : a + b, '-' : a - b, '*' : a * b, '/' : a / b } print(result.get('+'))#5 #方式二: def add(x,y): return x+y def sub(x,y): return x-y def mul(x,y): return x+y def div(x,y): return x/y operator = {'+':add, '-':sub, '*':mul, '/':div} def calc(x,o,y): print(operator.get(o)(x,y)) calc(2, '+', 3) #5