python 没有switch/case

python 没有switch/case,替代方法:

复制代码
def func_switch_case(product_name):
    """
    switch case 示范
    :param product_name:
    :return:
    """
    switcher = {
        "book": 1,
        "pencil": 2,
        "bag": 3
    }
    return switcher.get(product_name, "nothing") #nothing表示 case的default
def func_switch_case2(product_name):
    """
    switch case 示范
    :param product_name:
    :return:
    """
    result = 0
    if product_name == "book":
        result = 1001
    if product_name == "pencil":
        result = 1002
    if product_name == "bag":
        result = 1003
    return result


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    print_hi('good day')
    main()
    product_code = func_switch_case("bag")
    print(f"bag的产品编号:{product_code}")
    product_code = func_switch_case2("pencil")
    print(f"case2_pencil的产品编号:{product_code}")
复制代码

 

posted @   txwtech  阅读(80)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
历史上的今天:
2019-12-27 cc23b_demo-函数对象c++ 调用操作符的重载与函数对象-//用模板定义一元谓词、代码示范
点击右上角即可分享
微信分享提示