码迷,mamicode.com
首页 > 编程语言 > 详细

Python3实现购物车程序_高级

时间:2018-06-04 14:19:24      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:coding   dict   python   购物   bre   assets   item   需要   odi   

#!usr/bin/env python
# -*- coding:utf-8 -*-
assets = 0
li = input("请输入总资产:")
assets = li
goods = [
    {"name": "电脑", "price": 2000},
    {"name": "游艇", "price": 3000},
    {"name": "手机", "price": 1900},
    {"name": "美女", "price": 4000},
    {"name": "手表", "price": 1000},
]
for i in goods:
    print(i["name"],i["price"])
car_dict = {}
while True:
    i2 = input("请选择您需要购买的商品:")
    if i2.lower() == "y":
        break
    for itemd in goods:
        if itemd["name"] == i2:
            name = itemd["name"]
            if name in car_dict.keys():
                car_dict[name]["num"] = car_dict[name]["num"] + 1
            else:
                car_dict[name] = {"num": 1,"single_price": itemd["price"]}
print(car_dict)
#结算
car_all_price = 0
for k,v in car_dict.items():
    n = v["single_price"]
    m = v["num"]
    car_all_price_sum = n * m
    car_all_price = car_all_price + car_all_price_sum
print(assets,car_all_price)
if int(car_all_price) > int(assets):
    print("穷鬼!")
else:
    print("购买成功,咱回家!")

 

Python3实现购物车程序_高级

标签:coding   dict   python   购物   bre   assets   item   需要   odi   

原文地址:https://www.cnblogs.com/mengl950012/p/9132495.html

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