Python_简单三级菜单制作
一:制作要求
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("本次使用结束!")
2.使用列表
1 # 三级菜单 输入都是用一个变量 0120使用字典,列表 2 3 # 思路:while 4 # 通过定义列表进行选项存储 5 list=["北京","上海","香港","退出"] 6 list_1=["朝阳区","西城区","海淀区","返回上一级"] 7 list_1_1=["朝阳区_A","朝阳区_B","朝阳区_C","返回上一级"] 8 list_1_2=["西城区_A","西城区_B","西城区_C","返回上一级"] 9 while True: 10 # 输出一级菜单: 11 for i in list: 12 print(" \n%s: %s"%(list.index(i)+1,i)) 13 select_input = input("请输入你的选择:") 14 # 选项一 15 while select_input == "1": 16 for i in list_1: 17 print(" \n%s: %s" % (list_1.index(i) + 1, i)) 18 select_input_1 = input("请输入你的选择:") 19 while select_input_1 == "1": 20 for i in list_1_1: 21 print(" \n%s: %s" % (list_1_1.index(i) + 1, i)) 22 select_input_2 = input("请输入你的选择:") 23 if select_input_2 == "4": 24 break 25 while select_input_1 == "2": 26 for i in list_1_2: 27 print(" \n%s: %s" % (list_1_2.index(i) + 1, i)) 28 select_input_2 = input("请输入你的选择:") 29 if select_input_2 == "4": 30 break 31 while select_input_1 == "3": 32 for i in list_1_2: 33 print(" \n%s: %s" % (list_1_2.index(i) + 1, i)) 34 select_input_2 = input("请输入你的选择:") 35 if select_input_2 == "4": 36 break 37 if select_input_1 == "4": 38 break 39 # 选项二 40 while select_input == "2": 41 for i in list_1: 42 print(" \n%s: %s" % (list_1.index(i) + 1, i)) 43 select_input_1 = input("请输入你的选择:") 44 while select_input_1 == "1": 45 for i in list_1_2: 46 print(" \n%s: %s" % (list_1_2.index(i) + 1, i)) 47 select_input_2 = input("请输入你的选择:") 48 if select_input_2 == "4": 49 break 50 while select_input_1 == "2": 51 for i in list_1_2: 52 print(" \n%s: %s" % (list_1_2.index(i) + 1, i)) 53 select_input_2 = input("请输入你的选择:") 54 if select_input_2 == "4": 55 break 56 while select_input_1 == "3": 57 for i in list_1_2: 58 print(" \n%s: %s" % (list_1_2.index(i) + 1, i)) 59 select_input_2 = input("请输入你的选择:") 60 if select_input_2 == "4": 61 break 62 if select_input_1 == "4": 63 break 64 # 选项三 65 while select_input == "3": 66 for i in list_1: 67 print(" \n%s: %s" % (list_1.index(i) + 1, i)) 68 select_input_1 = input("请输入你的选择:") 69 while select_input_1 == 1: 70 for i in list_1_2: 71 print(" \n%s: %s" % (list_1_2.index(i) + 1, i)) 72 select_input_2 = input("请输入你的选择:") 73 if select_input_2 == "4": 74 break 75 while select_input_1 == "2": 76 for i in list_1_2: 77 print(" \n%s: %s" % (list_1_2.index(i) + 1, i)) 78 select_input_2 = input("请输入你的选择:") 79 if select_input_2 == "4": 80 break 81 while select_input_1 == "3": 82 for i in list_1_2: 83 print(" \n%s: %s" % (list_1_2.index(i) + 1, i)) 84 select_input_2 = input("请输入你的选择:") 85 if select_input_2 == "4": 86 break 87 if select_input_1 == "4": 88 break 89 # 选项四 90 if select_input == "4": 91 break 92 print("本次使用结束!")
使用列表运行效果和第一种方法类似,只不过把返回的值改成了4,
3.使用字典
# 三级菜单 输入都是用一个变量 0120使用字典,列表 # 思路:while # 通过字典进行选项存储 dic={ "1.北京":{ "1.朝阳区":["朝阳区_A","朝阳区_B","朝阳区_C","返回上一级"], "2.西城区":["西城区_A","西城区_B","西城区_C","返回上一级"], "3.海淀区":[], "4.返回上一级":[] }, "2.上海":{}, "3.香港":{}, "b.退出":{} } while True: # 输出一级菜单: print(dic.keys()) select_input = input("请输入你的选择:") # 选项一 while select_input == "1": print(dic["1.北京"].keys()) select_input_1 = input("请输入你的选择:") while select_input_1 == "1": print(dic["1.北京"]["1.朝阳区"]) select_input_2 = input("请输入你的选择:") if select_input_2 == "b": break while select_input_1 == "2": print(dic["1.北京"]["1.朝阳区"]) select_input_2 = input("请输入你的选择:") if select_input_2 == "b": break while select_input_1 == "3": print(dic["1.北京"]["1.朝阳区"]) select_input_2 = input("请输入你的选择:") if select_input_2 == "b": break if select_input_1 == "b": break # 选项二 while select_input == "2": print(dic["1.北京"].keys) select_input_1 = input("请输入你的选择:") while select_input_1 == "1": print(dic["1.北京"]["1.朝阳区"]) select_input_2 = input("请输入你的选择:") if select_input_2 == "b": break while select_input_1 == "2": print(dic["1.北京"]["1.朝阳区"]) select_input_2 = input("请输入你的选择:") if select_input_2 == "b": break while select_input_1 == "3": print(dic["1.北京"]["1.朝阳区"]) select_input_2 = input("请输入你的选择:") if select_input_2 == "b": break if select_input_1 == "b": break # 选项三 while select_input == "3": print(dic["1.北京"].keys) select_input_1 = input("请输入你的选择:") while select_input_1 == 1: print(dic["1.北京"]["1.朝阳区"]) select_input_2 = input("请输入你的选择:") if select_input_2 == "b": break while select_input_1 == "2": print(dic["1.北京"]["1.朝阳区"]) select_input_2 = input("请输入你的选择:") if select_input_2 == "b": break while select_input_1 == "3": print(dic["1.北京"]["1.朝阳区"]) select_input_2 = input("请输入你的选择:") if select_input_2 == "b": break if select_input_1 == "b": break # 选项四 if select_input == "b": break print("本次使用结束!")
四:运行效果截图(第一种方法):
五:注意事项
1.Python虽然简单,但是一定要熟练掌握格式,有的时候差一个空格都会出错而且还不容易排除错误.
2.关于语句:
select_input=b
while int(select_input)==1
运行报错:ValueError: invalid literal for int() with base 10: 'b',
语法错误
出错原因:Python3.0以后的版本不能进行字符串转换类型之后与数字之间的比较与判断
所以改成:select_input=="1"