# 1、启动程序后,让用户输入工资,然后进入循环,打印商品列表和编号 # 2、允许用户根据商品编号选择商品 # 3、用户选择商品后,检测余额是否够,够就直接扣款,并加入购物车, 不够就提醒余额不足 # 4、可随时退出,退出时,打印已购买商品和余额 # -*- encoding: utf-8 -*- goods = [ {"name": "电脑", "price": 1999}, {"name": "鼠标", "price": 10}, {"name": "游艇", "price": 20}, {"name": "美女", "price": 998} ] money = input('total money:').strip() while not money.isdigit(): print('input error! ') money = input('total money:').strip() money = int(money) for i in enumerate(goods): print(i) flag = True shop_list = [] cost = 0 res = money while flag: shops_add = input('select one thing:').strip() if shops_add.isdigit(): if int(shops_add) in range(len(goods)): print(type(goods[int(shops_add)]['name'])) shop_list.append(goods[int(shops_add)]['name']) cost += goods[int(shops_add)]['price'] res = money - cost if res < 0: flag = False print('余额不足') else: print('no select thing in the shop list!') elif shops_add.upper() == 'Q': flag = False print('you have bought these things:{0},your res is {1}'.format(shop_list,res))