#-*-coding:utf-8-*-
#总金额
Asset_all=0
i1=Input' Please enter total assets: ')
Asset_all=int (i1)
#商品列表
goods=[
{' Name ':Computer,' Price ':' 3999 '},
{' Name ':Mouse,' Price ':' 129 '},
{' Name ':Keyboard,' Price ':' 69 '},
{' Name ':' iphone ',' Price ':' 5388 '}
]
For IIn Goods:
Print (i[' Name '], i[' Price '])
car_dict={}#定义空字典存放购买的商品
# {
# ' name ': {' num ': ', ' price ': '} storage format
# }
While1:
I2=Input' Please select Product (y/y settlement): ')#选择购买的商品
If i2.lower () = =' Y ':
Break
For itemIn Goods:#循环商品列表, Item gets to product
If item[' Name ']==i2:#判断是否与输入一致
name=item[' Name ']#重新赋值
If nameIn Car_dict.keys ():#判断购买的商品在购物车中是否存在
car_dict[name][' Num ']= car_dict[name][' Num ']+1#如果存在, the number is +1.
Else
car_dict[name]={' Num ':1,' Single_price ': item[' Price ']}#商品在购物车中不存在就按照格式添加进来
Print (car_dict)#g购物车清单
#跳出循环去结算
All_price= 0 #购物总价
for k,v in car_dict.items ():
N=v[ ' single_price ']
M=v[ num ']
all_sum=m*n
All_price=all_price+int (all_sum)
if all_price<=asset_all:
print ( ' buy success ')
else:
print (
Python dictionary implements a simple shopping cart