不同取值下的函数调用--类似swithcase
每个取值都需要调用不同函数时,如果用ifelse实现可能代码比较长,python又没有switchcase,但可以用字典替代。
示例:
1 def cir(): 2 print("C is used") 3 4 def qui(): 5 print("Q is used") 6 7 switchs = {"C":cir, "Q":qui} 8 case = input("please input C or Q: ") 9 10 getFunc = switchs.get(case, "error") 11 if getFunc != "error": 12 getFunc()
结果:
1 E:\python\workspace>python -u "e:\python\workspace\Mooc\test.py" 2 please input C or Q: C 3 C is used 4 5 E:\python\workspace>python -u "e:\python\workspace\Mooc\test.py" 6 please input C or Q: Q 7 Q is used
浙公网安备 33010602011771号