Python3实现购物车程序_高级

Posted mengl950012

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python3实现购物车程序_高级相关的知识,希望对你有一定的参考价值。

#!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实现购物车程序_高级的主要内容,如果未能解决你的问题,请参考以下文章

day01-homework_购物车程序

Python3 高级特性

03-python3.5-模拟购物车流程--更新追加细节注释功能

Python学医第二天——购物车程序

Python3_基础部分_第一个Python程序

python3.5 购物车