Day 16 购物车

Posted

tags:

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

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# __author__ = "DaChao"
# Date: 2017/6/7

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# __author__ = "DaChao"
# Date: 2017/6/10

‘‘‘
要求用户输入总资产,例如:2000
显示商品列表,让用户根据序号选择商品,加入购物车
购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。
附加:可充值、某商品移除购物车
goods = [
    {"name": "电脑", "price": 1999},
    {"name": "鼠标", "price": 10},
    {"name": "游艇", "price": 20},
    {"name": "美女", "price": 998},
]
‘‘‘

def shangpin():
    ‘‘‘
    打印商品列表及序号,并价格赋值
    :return:
    ‘‘‘
    num = 1
    for n_p in goods:
        print(num,n_p["name"],n_p[price])
        p_goods.append(n_p[price])
        num+=1

def choose_1():
    ‘‘‘
    打印购物选择
    :return:
    ‘‘‘
    tips_1 = ["Continue shopping!", "Go payment!"]
    num = 1
    for n_p in tips_1:
        print(num,tips_1[num-1])
        num+=1

def choose_2():
    ‘‘‘
    打印金额不足选择
    :return:
    ‘‘‘
    tips_2 = ["Go recharge!","Go to remove goods!"]
    num = 1
    print("Your money are not enough!")
    for n_p in tips_2:
        print(num, tips_2[num - 1])
        num += 1

def goods_list():
    ‘‘‘
    显示购物车功能
    :return:
    ‘‘‘
    if goods_1 != 0:
        print("已购买电脑数量: ",goods_1)
    if goods_2 != 0:
        print("已购买鼠标数量: ",goods_2)
    if goods_3 != 0:
        print("已购买游艇数量: ",goods_3)
    if goods_4 != 0:
        print("已购买美女数量: ",goods_4)

if __name__ == __main__:
    goods = [
        {"name": "电脑", "price": 1999},
        {"name": "鼠标", "price": 10},
        {"name": "游艇", "price": 20},
        {"name": "美女", "price": 998},
    ]
    p_goods = []
    money_bsum = 0
    g_goods = []
    goods_1 = 0
    goods_2 = 0
    goods_3 = 0
    goods_4 = 0
    num = 0

    money_sum = int(input("Please input your total money in your wallet: "))

    tag_1 = True
    while tag_1:                  #商品购物
        shangpin()
        num = int(input("Please choose your favorite goods of id: "))
        if num in range(1,5):       #选择商品,购物总价叠加,并加入新购物列表,以便移除.
            money_bsum = money_bsum + int(p_goods[num-1])
            if num == 1:
                goods_1 += 1
                print("电脑 加入购物车!目前数量: ", goods_1)
            elif num == 2:
                goods_2 += 1
                print("鼠标 加入购物车!目前数量: ", goods_2)
            elif num == 3:
                goods_3 += 1
                print("游艇 加入购物车!目前数量: ", goods_3)
            elif num == 4:
                goods_4 += 1
                print("美女 加入购物车!目前数量: ", goods_4)
        else:
            print("Please input correct ID.")
        choose_1()
        choose_id = input("1 & 2 ?")
        if choose_id == "2":
            tag_1 = False

    tag_2 = True
    while tag_2:                 #支付选择,如果余额不足,充值或移除商品至支付成功。
        if money_bsum <= money_sum:
            print("Pay successful!")
            tag_2 = False
        else :
            choose_2()
            choose_id = input("1 & 2 ?")
            if choose_id == "1":
                money_sum += int(input("Please input your recharge money: "))
            elif choose_id == "2":
                goods_list()
                shangpin()
                while tag_2:
                    num_del = int(input(print("Please choose your del goods of id: ")))
                    if num_del == 1 and goods_1 != 0:
                        goods_1 -= 1
                        money_bsum -= 1999
                        if money_bsum <= money_sum:
                            tag_2 = False
                    elif num_del == 2 and goods_2 != 0:
                        goods_2 -= 1
                        money_bsum -= 10
                        if money_bsum <= money_sum:
                            tag_2 = False
                    elif num_del == 3 and goods_3 != 0:
                        goods_3 -= 1
                        money_bsum -= 20
                        if money_bsum <= money_sum:
                            tag_2 = False
                    elif num_del == 1 and goods_4 != 0:
                        goods_4 -= 1
                        money_bsum -= 998
                        if money_bsum <= money_sum:
                            tag_2 = False
                print("You can offord your shopping!")

 

以上是关于Day 16 购物车的主要内容,如果未能解决你的问题,请参考以下文章

(转)Python作业day2购物车

day01课程回顾,数据类型,(用户登录限制登录三次,购物车,省市县三级联动)

day2购物商城

DAY4:简单购物系统

day06-jsp

web day25 web day24 小项目练习图书商城, 购物车模块,订单模块,支付(易宝支付)