一:制作要求
1.三级菜单
2.可依次选择进入各子菜单
3.所需新知识点:字典,列表
*本文通过三种方法完成,第一种:只使用循环,第二种:使用列表,第三种:使用字典
二:FlowChart流程图
与上图对应,实现方式图解:
while用来判断输入的数据和允许输入数据中的哪一项匹配,if来进行判断是否退出本次循环,
三:具体实现代码:
1.没有使用列表字典
1 #三级菜单 输入都是用一个变量 0120使用字典,列表 2 3 #思路:while 4 #定义一级菜单 5 msg=''' 6 请输入你要进行的操作前面的索引(1,2,3,b): 7 8 1.北京 9 2.上海 10 3.香港 11 b.退出 12 ''' 13 #定义二级菜单 14 msg_1=''' 15 1.朝阳区 16 2.西城区 17 3.海淀区 18 b.返回上一级 19 ''' 20 msg_2_2=''' 21 1.西城区_A 22 2.西城区_B 23 3.西城区_C 24 b.返回上一级 25 ''' 26 #定义三级菜单 27 msg_2_1=''' 28 1.朝阳区_A 29 2.朝阳区_B 30 3.朝阳区_C 31 b.返回上一级 32 ''' 33 while True: 34 #输出一级菜单: 35 print(msg) 36 select_input=input("请输入你的选择:") 37 #选项一 38 while select_input== "1": 39 print(msg_1) 40 select_input_1 = input("请输入你的选择:") 41 while select_input_1 == "1": 42 print(msg_2_1) 43 select_input_2 = input("请输入你的选择:") 44 if select_input_2=="b": 45 break 46 while select_input_1 == "2": 47 print(msg_2_2) 48 select_input_2 = input("请输入你的选择:") 49 if select_input_2 == "b": 50 break 51 while select_input_1 == "3": 52 print(msg_2_2) 53 select_input_2 = input("请输入你的选择:") 54 if select_input_2 == "b": 55 break 56 if select_input_1== "b": 57 break 58 #选项二 59 while select_input == "2": 60 print(msg_1) 61 select_input_1 = input("请输入你的选择:") 62 while select_input_1 == "1": 63 print(msg_2_1) 64 select_input_2 = input("请输入你的选择:") 65 if select_input_2 == "b": 66 break 67 while select_input_1 == "2": 68 print(msg_2_2) 69 select_input_2 = input("请输入你的选择:") 70 if select_input_2 == "b": 71 break 72 while select_input_1 == "3": 73 print(msg_2_2) 74 select_input_2 = input("请输入你的选择:") 75 if select_input_2 == "b": 76 break 77 if select_input_1 == "b": 78 break 79 #选项三 80 while select_input == "3": 81 print(msg_1) 82 select_input_1 = input("请输入你的选择:") 83 while select_input_1 == 1: 84 print(msg_2_1) 85 select_input_2 = input("请输入你的选择:") 86 if select_input_2 == "b": 87 break 88 while select_input_1 == "2": 89 print(msg_2_2) 90 select_input_2 = input("请输入你的选择:") 91 if select_input_2 == "b": 92 break 93 while select_input_1 == "3": 94 print(msg_2_2) 95 select_input_2 = input("请输入你的选择:") 96 if select_input_2 == "b": 97 break 98 if select_input_1 == "b": 99 break 100 #选项四 101 if select_input=="b": 102 break 103 print("本次使用结束!")