欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python3实现购物车程序_高级

程序员文章站 2022-04-04 15:30:15
#!usr/bin/env python # -*- coding:utf-8 -*- assets = 0 li = input("请输入总资产:") assets = li goods = [ {"name": "电脑", "price": 2000}, {"name": "游艇", "pric... ......
#!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("购买成功,咱回家!")