python 购物车项目小练习

# -*- coding: utf-8 -*-
import time

#  展品展示
goods_list = [
    {"name": "电脑", 'price': 1999, "num": 20, "sum": "有现货"},
    {"name": "鼠标", 'price': 10, "num": 20, "sum": "有现货"},
    {"name": "游艇", 'price': 20, "num": 20, "sum": "有现货"},
    {"name": "美女", 'price': 998, "num": 20, "sum": "有现货"},
    {"name": "油精", 'price': 30, "num": 20, "sum": "有现货"}
]

# 添加购物车用
my_car = {
    1: {"name": "电脑", 'price': 1999, "num1": 0, "sum": "有现货"},
    2: {"name": "鼠标", 'price': 10, "num1": 0, "sum": "有现货"},
    3: {"name": "游艇", 'price': 20, "num1": 0, "sum": "有现货"},
    4: {"name": "美女", 'price': 998, "num1": 0, "sum": "有现货"},
    5: {"name": "油精", 'price': 30, "num1": 0, "sum": "有现货"}
}

# 充值金额函数体
chu_money = 0  # 初始化金额


def chongzhi():
    global chu_money
    tag = True
    while tag:
        money = input("请输入您要充值的金额").strip()
        if money.isdigit():  # isdigit(判断是否由数字组成)
            money = int(money)
            chu_money += money
            print("充值成功,您当前的余额为{}".format(chu_money))
            break
        else:
            print("充值失败,请正确输入金额数字")


# 展品展示
def shwo_shangpin():
    print("====================================================================================")
    print("%-5s %-20s %-15s %-15s %-20s" % ("商品编号", "商品名称", "商品价格", "商品数量", "是有现货"))
    for index, dic in enumerate(goods_list, start=1):
        print("%-9s %-20s %-20s %-20s %-8s" % (index, dic["name"], dic["price"], dic["num"], dic["sum"]))
    print("====================================================================================")


# 添加购物车
def shopping(res):
    # 如果购物车已经存在,数量加一
    if goods_list[res-1]['num'] <=0:
        print("购物车中商品:%(name)s 数量:%(num)s 已经到库存上限,抱歉!!" % goods_list[res-1])
        return
    if res in my_car:
        my_car[res]["num1"] += 1
        goods_list[res - 1]["num"] -= 1

    # if goods_list[res1
    else:
        # 如果没有创建一个条新记录
        my_car[res] = {
            "name": goods_list[res - 1]["name"],
            "price": goods_list[res - 1]["price"],
            "num1": 1
        }
    print("====================================================================================")
    print(">>>>>已成功添加购物车")
    print("%-5s %-20s %-15s %-15s %-20s" % ("商品编号", "商品名称", "商品价格", "商品数量", "是否有现货"))

    print("%-9s %-20s %-20s %-20s %-8s" % (
    res, my_car[res]["name"], my_car[res]["price"], str(str(goods_list[res - 1]["num"]) + '|' + str(my_car[res]["num1"])),
    my_car[res]["sum"]))
    print()
    print("请您确认目前你数量为:%s件,购买产品总价为:%d元,您当前余额为:%d元,请您先核对金额后再确认购买此产品?" % (
    my_car[res]["num1"], my_car[res]["price"] * int(str(my_car[res]["num1"])), chu_money))
    print("====================================================================================")


# 退出购买中心
def quit():
    print('欢迎下次光临购物平台')


# 删除商品
def del_shopping(res):
    if res.isdigit():
        res = int(res)
        if 0 < res <= len(goods_list):
            my_car[res]["num1"] -= 1
            if my_car[res]["num1"] == 0:
                my_car.pop(res)
                a = jiesuan()
                print(a)
        else:
            print("请正确输入编码")
    else:
        print("没有这个选项")


# 结算购物车
def jiesuan():
    global tag
    while 1:
        print("====================================================================================")
        print("%-5s %-20s %-15s %-15s %-20s" % ("商品编号", "商品名称", "商品单价", "商品数量", "购买总价"))
        filter = 0
        money2 = 0
        for index, dic in my_car.items():
            if dic["num1"] != 0:
                text = dic["price"] * dic["num1"]
                filter += dic["num1"]
                money2 += text
                print("%-9s %-20s %-20s %-20s %-8s" % (index, dic["name"], dic["price"], dic["num1"], text))
        print("请您确认目前你数量为:%s件,购买产品总价为:%d元,您当前余额为:%d元,请您先核对金额后再确认购买此产品?" % (filter, money2, chu_money))
        print("====================================================================================")
        tag1 = True
        while tag1:
            if chu_money - money2 >= 0:
                name = input("'确认购买请按y,按q结束程序'")
                if name.upper() == "Y":
                    print('''
					  ********************************************
					  *       "购买成功,您还还剩余%s元"             *
					  *		                                     *
					  ********************************************
					''' % (chu_money - money2))
                    time.sleep(3)
                    break
                elif name.upper() == "":
                    print("请输入正确编号,购买请按y,按q结束程序")
                elif name.upper() == "Q":
                    quit()
                    break

                else:
                    pass
            else:
                # 提示余额不足提醒
                print('''
				您的余额已不足,您当前金额只有:%s元,请痛心删掉一些产品或充充值>>>>:

				33充值:	C      33删除:	d		33退出:	q''' % (chu_money))
                print("====================================================================================")
                print()

                # 删除商品

                name2 = input("删除一些产品输入:d		充值请输入:c		退出程序输入:q").strip()
                if name2.upper() == "D":
                    res = input("请输入您要删除的产品编号:	").strip()
                    del_shopping(res)
                    jiesuan()
                elif name2.upper() == "C":
                    chongzhi()
                elif name2.upper() == "	Q":
                    quit()
                    break
                tag = False
        break


def error():
    print("请正确输入选项")


# *************************************购物车主程序***************************************
if __name__ == '__main__':
    print(''' 
*********************************************************************************
*                                                                               *
*                            欢迎来到huangjiangyong购物平台                       *
*                                                                               *
*********************************************************************************
''')
    # 1.充金额调用
    chongzhi()
    # 2.商品展示
    shwo_shangpin()
    # 3开始购物
    tag = True
    while tag:
        res = input("请输入您要购买的产品编号:按n结算,按q退出")
        if res.isdigit():
            res = int(res)
            if 0 < res <= len(goods_list):
                shopping(res)  # 调用-> 添加购物车
            else:
                print("你输入的数字已超范围")

        elif res.upper() == "Q":
            quit()
            break
        elif res.upper() == "N":
            jiesuan()
        else:
            print("无法识别到您输入的编号请从新输入")

  

原文地址:https://www.cnblogs.com/huangjiangyong/p/10923809.html