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}")
欢迎讨论,相互学习。
cdtxw@foxmail.com
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
2019-12-27 cc23b_demo-函数对象c++ 调用操作符的重载与函数对象-//用模板定义一元谓词、代码示范