码迷,mamicode.com
首页 > 其他好文 > 详细

13 购物车之二(用字典)

时间:2018-10-27 19:50:57      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:dict   ems   break   字典   else   商品   name   item   电脑   

asset_all = eval(input("请输入总资产:"))
car_dict = {}
# dict_list = {
#     "电脑":{"single_price":"单价","num":"个数"}
#
# }
goods = [
    {"name":"电脑","price": 1999},
    {"name":"鼠标","price": 10},
    {"name":"游艇","price": 20},
    {"name":"美女","price": 998},
    ]
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:
            name = item[‘name‘]
            if name in car_dict.keys():
                car_dict[name]["num"] += 1
            else:
                car_dict[name] = {"single_price": item["price"], "num": 1}
print(car_dict)
sum_all = 0
for k,v in car_dict.items():
    print(k,v)
    n = v[‘single_price‘]
    m = v[‘num‘]
    sum_all += m*n
print(sum_all)
if asset_all < sum_all:
    print("余额不足")
else:
    print("购买成功")

13 购物车之二(用字典)

标签:dict   ems   break   字典   else   商品   name   item   电脑   

原文地址:https://www.cnblogs.com/wssaried/p/9862697.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!