牛刀小试-购物车
#author:张浩 #date:2019/8/14 list_goods = [ ['iphone6s',5800], ['mac book',9000], ['coffee',30], ['python book',80], ['bicycle',1500] ] while True: salary = input('请输入充值金额:') shopping_car = [] if salary.isdigit(): salary = int(salary) while True: print("以下是商品列表:") for goods in list_goods: print(list_goods.index(goods)+1,",",goods) choice = input("请输入想购买的商品序列号,输入q退出:") if choice.isdigit(): choice = int(choice) if 0 < choice <= len(list_goods): if list_goods[choice-1][1] <= salary: shopping_car.append(list_goods[choice - 1]) print("已将%s商品加入购物车"%list_goods[choice-1]) salary = salary - list_goods[choice-1][1] else : print("余额不足,剩余钱:%s"%salary) else: print("输入的商品序号不存在!") elif choice == "q": print("已购买如下商品:") for goods_buy in shopping_car: print(shopping_car.index(goods_buy)+1,goods_buy) print("余额:%s"%salary) break else: print("输入有误,请重新输入!") break else: print("输入有误,请重新输入!")