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