题目:
多级菜单
1.三级菜单
2.可依次选择进入的各子菜单
3.所需新知识点:列表、字典
ReadMe:
这个做循环,比较绕脑子
点开运行即可
Mind Map:
代码:
#!/usr/bin/env python
#py3.5
#2018.02.06
#-*- Coding:utf-8 -*-
# Author:Eric.Shen
#多级菜单
#1.三级菜单
#2.可依次选择进入的各子菜单
#3.所需新知识点:列表、字典 #自己建立一个三级字典
menu = {
"衣服":{
"男士":["上衣","下衣","鞋包","皮带"],"女士":["上衣","裤子","裙子","鞋包"],"儿童":["上衣","下衣","鞋包"]},
"汽车":{
"精品车":["宝马","奔驰","路虎"],"二手车":["大众","本田","荣威"]},
"食物":{"肉":["肥肉","瘦肉","肥瘦"]}
} #将三级菜单的key赋值product_list
product_list = list(menu.keys())#获得一级菜单
while True:
print("菜单".center(50,"*"))
for line in product_list:
print(product_list.index(line),line)
print("------------------")
choice = input("请输入编号或者输入q退出b返回:")
if choice.isdigit():
choice = int(choice)
if choice >= 0 and choice < len(product_list):
choice_name = product_list[choice]#获得第一级Id
product = list(menu[choice_name].keys())#获得选择商品的下一级菜单
while True:
print("商品".center(50,"*"))
for line2 in product:#打印二级菜单
print(product.index(line2),line2)
print("----------------")
choice_product = input("请输入编号或者输入q退出b返回:")
if choice_product.isdigit():
choice_product = int(choice_product)
if choice_product >= 0 and choice_product < len(product_list):
choice_name2 = product[choice_product]#获得第二级的Id
choice_name2_list = menu[choice_name][choice_name2]#获得第二级菜单
while True:
print("衣物".center(50,"*"))
for line3 in choice_name2_list:
print(choice_name2_list.index(line3),line3)
back_or_quit = input("返回输入b,退出输入q")
if back_or_quit == "b":
break
elif back_or_quit == "q":
exit()
else:
print("输入错误!")
else:
print("输入%d不存在"%(choice_product))
elif choice_product == "q":
exit()
elif choice_product == "b":
break
else:
print("输入非法!")
else:
print("输入%d不存在"%(choice))
elif choice == "q":
exit()
elif choice == "b":
break
else:
print("输入非法!")
欢迎指错