python中没有switch-case语句

python没有switch-case语句,查看官方文档说是可以用if-elseif-elseif。。。。代替。

Python推崇的方法来处理switch/case问题,一般可以通过字典来处理这种多分支的问题,举例说明。

def switch_case(key0):
    mapping = {
        'cn': 'abc000',
        'us': 100
    }
    print('内---')
    result = mapping.get(key0, 3)  # key0:字典中要查找的键。 后面的3是default,如果指定的键不存在时,则返回该值,默认为None
    return result


print(switch_case('cn'))

输出结果:

原文地址:https://www.cnblogs.com/may18/p/14536413.html