三级菜单
要求:
打印省、市、县三级菜单 可返回上一级 可随时退出程序
购物车程序
要求:
用户名和密码存放于文件中,格式为:egon|egon123 启动程序后,先登录,登录成功则让用户输入工资,然后打印商品列表,失败则重新登录,超过三次则退出程序 允许用户根据商品编号购买商品 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 可随时退出,退出时,打印已购买商品和余额
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
menu = { '北京':{ '海淀':{ '五道口':{ 'soho':{}, '网易':{}, 'google':{} }, '中关村':{ '爱奇艺':{}, '汽车之家':{}, 'youku':{}, }, '上地':{ '百度':{}, }, }, '昌平':{ '沙河':{ '老男孩':{}, '北航':{}, }, '天通苑':{}, '回龙观':{}, }, '朝阳':{}, '东城':{}, }, '上海':{ '闵行':{ "人民广场":{ '炸鸡店':{} } }, '闸北':{ '火车战':{ '携程':{} } }, '浦东':{}, }, '山东':{}, } #part1(初步实现):能够一层一层进入 layers = [menu, ] while True: current_layer = layers[-1] for key in current_layer: print(key) choice = input('>>: ').strip() if choice not in current_layer: continue layers.append(current_layer[choice]) #part2(改进):加上退出机制 layers=[menu,] while True: if len(layers) == 0: break current_layer=layers[-1] for key in current_layer: print(key) choice=input('>>: ').strip() if choice == 'b': layers.pop(-1) continue if choice == 'q':break if choice not in current_layer:continue layers.append(current_layer[choice])
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import os product_list = [['Iphone7',5800], ['Coffee',30], ['疙瘩汤',10], ['Python Book',99], ['Bike',199], ['ViVo X9',2499], ] shopping_cart={} current_userinfo=[] db_file=r'db.txt' while True: print(''' 登陆 注册 购物 ''') choice=input('>>: ').strip() if choice == '1': #1、登陆 tag=True count=0 while tag: if count == 3: print('