menu = {
'北京': {
'海淀': {
'五道口': {
'soho': {},
'网易': {},
'google': {}
},
'中关村': {
'爱奇艺': {},
'汽车之家': {},
'youku': {},
},
'上地': {
'百度': {},
},
},
'昌平': {
'沙河': {
'老男孩': {},
'北航': {},
},
'天通苑': {},
'回龙观': {},
},
'朝阳': {},
'东城': {},
},
'上海': {
'闵行': {
"人民广场": {
'炸鸡店': {}
}
},
'闸北': {
'火车站': {
'携程': {}
}
},
'浦东': {},
},
'山东': {},
}
- 可以依次选择进入个子菜单
- 可以从任意一层往回退到上一层
- 可以从任意一层退出程序
Flag = True
while Flag:
for i in menu:
print(i)
choice1 = input(">:").strip()
if not choice1: continue
if choice1 in menu:
while Flag:
for j in menu[choice1]:
print(j)
choice2 = input(">>:").strip()
if not choice2: continue
if choice2 in menu[choice1]:
while Flag:
for k in menu[choice1][choice2]:
print(k)
choice3 = input(">>>:").strip()
if not choice3: continue
if choice3 in menu[choice1][choice2]:
while Flag:
for q in menu[choice1][choice2][choice3]:
print(q)
choice4 = input(">>>>:").strip()
if choice4 in menu[choice1][choice2][choice3]:
while Flag:
for q in menu[choice1][choice2][choice3][choice4]:
print(q)
elif choice4 == 'b':
break
else:
print("未找到")
elif choice3 == 'b':
break
else:
print("未找到")
elif choice2 == 'b':
break
else:
print("未找到")
elif choice1 == 'b':
break
else:
print("未找到")
current_layer = menu
layers = []
Flag = True
while Flag:
for i in current_layer:
print(i)
choice = input(">:").strip()
if not choice: continue
if choice in current_layer:
layers.append(current_layer)
# print(layers)
current_layer = current_layer[choice]
elif choice == 'b':
if len(layers) != 0:
current_layer = layers.pop(-1)
else:
print("已经是最顶层了!")
现有商品列表如下:
products=[[‘iPhone8’,6888],[‘MacPro’,14800],[‘小米6’,2499],[‘Coffee’,31],[‘Book’,80],[‘Nike Shoes’,799]]
需要打印这样的格式:
---------商品列表----------
0.iPhone8 6888
1.MacPro 14888
2.小米6 2499
3.Coffee 31
4.Book 80
5.Nike Shoes 799
升级:写一个循环,不断问用户想买什么,用户选择一个商品编号,就把对应的商品添加到购物车里,最终用户输入q退出时,打印购物车里的商品列表。
products=[['iPhone8', 6888], ['MacPro', 14800], ['小米6', 2499], ['Coffee',31], ['Book', 80], ['Nike Shoes', 799]]
shopping_cart = []
# run_flag = True #运行标志
exit_flag = False #退出标志
while not exit_flag:
print("---------商品列表--------")
for index,p,in enumerate(products):
print("%s. %s %s" %(index, p[0], p[1]))
choice = (input("输入想买的商品编号:"))
if choice.isdigit():
choice = int(choice)
if choice >= 0 and choice < len(products):
shopping_cart.append(products[choice])
print("Add product %s into shopping_cart."%products[choice])
else:
print("商品不存在!")
elif choice == 'q':
if len(shopping_cart) > 0:
print("---------你已购买以下商品-------")
for index,p in enumerate(shopping_cart):
print("%s. %s %s" % (index, p[0], p[1]))
#break
#run_flag = False
exit_flag = True