1. 程式人生 > >python作業5:多級菜單

python作業5:多級菜單

多級 主程序 一次循環 rate 循環 數字 print 次循環 輸入

# 作業5:多級菜單
# ● 三級菜單,省、市、縣、公司,要求程序啟動後,允許用戶依次選擇進入各子菜單
# ● 可以在任意一級菜單返回上級菜單,也可以退出程序
# ● 所需新知識點:列表、字典
#
# 思路:
# 先創字典,字典三層嵌套
#逐層提取key字段,
#先不管各種可能,把主程序寫好,即各種選擇都是好好的情況下能實現的功能,再補充各種可能
#這個地方其實是重點考察while循環裏面的continue和break
#出於程序簡化,沒有考慮輸入數字超範圍、輸入的不是數字、q、b三者以外的情況

dict1 ={
        "省1":{
            "市11":{
                
"縣111":{ "公司1111:{}", "公司1112:{}" }, "縣112":{ "公司1121:{}", "公司1122:{}" } }, "市12":{ "縣121":{ "公司1211:{}",
"公司1212:{}" }, "縣122":{ "公司1221:{}", "公司1222:{}" } }, "市13":{ "縣131":{},"縣132":{} } }, "省2":{ "市21":{ "縣211":{},"縣212
":{} }, "市22":{ "縣221":{},"縣222":{} }, "市23":{ "縣231":{},"縣232":{} } }, "省3":{ "市31":{ "縣311":{},"縣312":{} }, "市32":{ "縣321":{},"縣222":{} }, "市33":{ "縣331":{},"縣332":{} } } } judge = True while judge: list1 = list(dict1.keys()) list1.sort() print("The level_1 menu:") for index,item in enumerate(list1): print(index,item) choice1 = input("Please choose level_1:") if choice1.isdigit(): choice1 = int(choice1) choice_key1 = list1[choice1] list2 = list(dict1.get(choice_key1)) list2.sort() while judge: print("The level_2 menu:") for index, item in enumerate(list2): print(index, item) choice2 = input("Please choose level_2:") if choice2.isdigit(): choice2 = int(choice2) choice_key2 = list2[choice2] list3 = list(dict1.get(choice_key1).get(choice_key2)) list3.sort() while judge: print("The level_3 menu:") for index, item in enumerate(list3): print(index, item) choice3 = input("Please choose level_3:") if choice3.isdigit(): choice3 = int(choice3) choice_key3 = list3[choice3] list4 = list(dict1.get(choice_key1).get(choice_key2).get(choice_key3)) list4.sort() print("The level_4 menu:") for index, item in enumerate(list4): print(index, item) choice4 = input("It‘s the last level,Please come back:") if choice4 == "q": # 判斷位置空後,可以連續跳出多個循環 judge = False # 這裏是跳出了本while循環的所有循環 break elif choice4 == "b": # 這裏只是跳出了本while循環中的一次循環 continue elif choice3 == "q": judge = False break elif choice3 == "b": break elif choice2 == "q": judge = False break elif choice2 == "b": break elif choice1 == "q" :#是q退出 judge = False else:#不是q也不是數字,重新選 print("Please choose the number.")

python作業5:多級菜單