用python实现简单购物车功能
all_asset = 0 i1 = input("请输入总资产:") all_asset = int(i1) goods = [ {'name':'电脑','price':1999}, {'name':'鼠标','price':10}, {'name':'油田','price':20}, {'name':'美女','price':998} ] for i in goods: print(i['name'],i['price']) car_dict = {} while True: i2 = input('请选择商品(Y/结算):') if i2 =="y": break for item in goods: if item['name'] == i2: name = item['name'] if name in car_dict.keys(): car_dict[name]['num'] = car_dict[name]['num'] + 1 else: car_dict[name] = {'num': 1,'single_price': item['price']} print(car_dict) print(car_dict) all_price = 0 for v in car_dict.values(): m = v['num'] n = v['single_price'] all_sum = m * n all_price = all_price + all_sum if all_price >all_asset: print('穷逼','总价:'+ str(all_price),'余额:'+ str(all_asset-all_price)) else: print('土豪','总价:'+ str(all_price),'余额:'+ str(all_asset-all_price))