#简易版购物车#__author: liangchen Date: 2019/8/12 things = [ ('iphone6s',5800), ('mac book',9000), ('coffee',32), ('python boook',80), ('bicycle',1500) ] saving = input("please input your saving:") #拥有的本金 shopping_car = [] if saving.isdigit(): saving = int(saving) while True: #打印商品内容 for i,v in enumerate(things,1): print(i,'---',v) thing_num = input("请选择你要购买商品的编号(退出:q):") #验证购买的是否是已有商品 if thing_num.isdigit(): thing_num = int(thing_num) if thing_num > 0 and thing_num <= len(things): thing = things[thing_num-1] #判断是否有足够余额,如果有,则余额为本金减去该商品的价格并加入购物车 if thing[1] < saving: saving -= thing[1] shopping_car.append(thing) print(thing,"已加入购物车") else: print("余额不足,还剩%s"%saving) else: print("error Item number") elif thing_num == 'q': print("-------您的购物车有如下商品--------") #循环遍历购物车里的商品 for i in shopping_car: print(i) print("您还剩余额 %s元"%saving) break else: print("Not this thing!") else: print("Invalid saving!")
来源:https://www.cnblogs.com/liang-chen/p/11342045.html