简易python购物车

#!/usr/bin/env python
# -*- coding:utf-8 -*-
asset_all=0
i1 = input("请输入总资产:")
asset_all=int(i1)
goods=[
    {"name":"电脑","price":200},
    {"name":"手机","price":10},
    {"name":"鼠标","price":20}
]
car_list={}
for i in goods:
    print(i["name"],i["price"])
while True:
    i2=input("亲选择商品(结算y/Y):")
    if i2.lower() == "y":
        break
    for item in goods:
        if item["name"]==i2:
            if i2 in car_list.keys():
                car_list[i2]["num"]+=1
            else:
                car_list[i2]={"num": 1,"single_price":item["price"]}

print(car_list)
all_sum=0
for k,v in car_list.items():
   count=v["num"]
   s_price=v["single_price"]
   all_sum = count*s_price+all_sum
print(all_sum)
if all_sum > asset_all:
    print("买不起")
else:
    print("买得起")

购物车简单实现,主要是使用字典功能

posted @ 2016-10-29 15:37  solakevon  阅读(197)  评论(0编辑  收藏  举报