標籤:list 列表 last int while迴圈 for 多個 返回 choice
# 作業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 = Truewhile 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:多級菜單