# 作业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.")
原文地址:https://www.cnblogs.com/welljoy/p/8274136.html
时间: 2024-11-09 02:21:00