Python之switch

首先声明,Python没有switch!!!

通过函数与字典的结合实现

 1 #!/usr/bin/python
 2 #coding:utf-8
 3 
 4 from __future__ import division
 5 
 6 def jia(x,y):
 7         return x+y 
 8 
 9 def jian(x,y):
10         return x-y 
11 
12 def cheng(x,y):
13         return x*y 
14 
15 def chu(x,y):
16         return x/y 
17 
18 operator = {"+":jia, "-":jian, "*":cheng, "/":chu}
19 
20 def f(x,o,y):
21         print operator.get(o)(x,y)
22 
23 f(3,'/',9)

 

posted on 2017-06-10 10:37  枝桠  阅读(215)  评论(0编辑  收藏  举报

导航