一、购物车程序
需求:
- 启动程序后,让用户输入工资,然后打印商品列表
- 允许用户根据商品编码购买商品
- 用户选择商品后检测余额是否够,够就直接扣款,不够就提醒
- 用户可以一直购买商品,也可以随时退出,退出时,打印已购买的商品和余额
shoplist = [] commodity = [[‘iphonex‘,8888],[‘Book‘,99],[‘Macpro‘,7666],[‘小米6‘,3200]] long = len(commodity) wages = input("请输入你的工资--->") if wages.isdigit() ==True: wages = int(wages) print(‘你的工资是‘,wages,‘元‘) while True: print(‘--------商品列表-------‘) for y, p in enumerate(commodity): print("{0} {1} \t{2}".format(y, p[0], p[1], ‘元‘)) need = input("请输入你需要购买的商品编号---->") if need == ‘q‘ or need == ‘Q‘: print(‘退出不买了‘) print("-----你的购物车-----") for y, p in enumerate(shoplist): print(y, p[0], p[1]) # wages = wages - commodity[need][1] print("你剩余", wages, "元") break elif need.isdigit() == True: need = int(need) if wages >= commodity[need][1]: need = int(need) elif wages < commodity[need][1]: print("骚年去赚钱吧,你还剩", wages, "元了,还想买", commodity[need][0]) continue if need >= 0 and need <= long: print(‘你购买了‘, commodity[need]) shoplist.append(commodity[need]) print("-----你的购物车-----") for y, p in enumerate(shoplist): print(y, p[0], p[1]) wages = wages - commodity[need][1] print("你剩余", wages, "元") else: print("你输入的什么玩意,请输入数字") continue else: print("你输入的什么玩意,让你输入数字")
二、多级菜单
需求:
- 现有省市县三级结构,要求程序启动后,允许用户可依次选择进入各级子菜单,
- 可在任意一级菜单返回上一级
- 可以在任意一级菜单退出程序
menu = { ‘河北‘:{ ‘承德‘:{ ‘双桥区‘:‘贾营村‘ } }, ‘北京‘:{ ‘通州区‘:{ ‘东潞苑小区‘,‘我家‘ } } } while True: print(menu.keys()) choice = input("请输入你要查看的省份---") if menu.get(choice) != None: sub = menu[choice] #print(sub.keys()) while True: print(sub.keys()) choice_sub = input("请输入你要查看的市,或者返回上一级请输入R") if choice_sub == ‘r‘ or choice_sub == ‘R‘: break elif choice_sub == ‘q‘ or choice == ‘Q‘: print("程序退出") exit() elif sub.get(choice_sub) != None: print(sub[choice_sub]) sub_sub = input("是否返回上一层,R=返回,e=退出") if sub_sub == ‘r‘ or sub_sub == ‘R‘: print(‘返回上一层‘) else: print(‘退出‘) exit() elif choice == ‘q‘ or choice == ‘Q‘: print("程序退出") exit()