Python学习-------------------三级菜单简单版
题目:
多级菜单
1.三级菜单
2.可依次选择进入的各子菜单
3.所需新知识点:列表、字典
ReadMe:
这个做循环,比较绕脑子
点开运行即可
Mind Map:
代码:
1 #!/usr/bin/env python 2 #py3.5 3 #2018.02.06 4 #-*- Coding:utf-8 -*- 5 # Author:Eric.Shen 6 #多级菜单 7 #1.三级菜单 8 #2.可依次选择进入的各子菜单 9 #3.所需新知识点:列表、字典 10 11 #自己建立一个三级字典 12 menu = { 13 "衣服":{ 14 "男士":["上衣","下衣","鞋包","皮带"],"女士":["上衣","裤子","裙子","鞋包"],"儿童":["上衣","下衣","鞋包"]}, 15 "汽车":{ 16 "精品车":["宝马","奔驰","路虎"],"二手车":["大众","本田","荣威"]}, 17 "食物":{"肉":["肥肉","瘦肉","肥瘦"]} 18 } 19 20 #将三级菜单的key赋值product_list 21 product_list = list(menu.keys())#获得一级菜单 22 while True: 23 print("菜单".center(50,"*")) 24 for line in product_list: 25 print(product_list.index(line),line) 26 print("------------------") 27 choice = input("请输入编号或者输入q退出b返回:") 28 if choice.isdigit(): 29 choice = int(choice) 30 if choice >= 0 and choice < len(product_list): 31 choice_name = product_list[choice]#获得第一级Id 32 product = list(menu[choice_name].keys())#获得选择商品的下一级菜单 33 while True: 34 print("商品".center(50,"*")) 35 for line2 in product:#打印二级菜单 36 print(product.index(line2),line2) 37 print("----------------") 38 choice_product = input("请输入编号或者输入q退出b返回:") 39 if choice_product.isdigit(): 40 choice_product = int(choice_product) 41 if choice_product >= 0 and choice_product < len(product_list): 42 choice_name2 = product[choice_product]#获得第二级的Id 43 choice_name2_list = menu[choice_name][choice_name2]#获得第二级菜单 44 while True: 45 print("衣物".center(50,"*")) 46 for line3 in choice_name2_list: 47 print(choice_name2_list.index(line3),line3) 48 back_or_quit = input("返回输入b,退出输入q") 49 if back_or_quit == "b": 50 break 51 elif back_or_quit == "q": 52 exit() 53 else: 54 print("输入错误!") 55 else: 56 print("输入%d不存在"%(choice_product)) 57 elif choice_product == "q": 58 exit() 59 elif choice_product == "b": 60 break 61 else: 62 print("输入非法!") 63 else: 64 print("输入%d不存在"%(choice)) 65 elif choice == "q": 66 exit() 67 elif choice == "b": 68 break 69 else: 70 print("输入非法!")
欢迎指错
Powers a lot like real estate.Its all about location, location, location.The closer you are to the source,the higher your property value.