购物车

#!/usr/bin/env python
# -*- coding:utf-8 -*-  
# by wk

'''
购物车
功能要求:

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


# 充值模块
def recharge(userlist):
    flag1 = True  # 循环标记
    while flag1:
        flag2 = True  # 循环标记
        add_money = input('请输入充值金额(数字): ')
        if add_money.isdigit():
            old_money = int(userlist[0]['money'])
            new_money = old_money + int(add_money)
            users[0]['money'] = new_money
            print('您现在的余额是 %s元' % userlist[0]['money'])
            while flag2:
                readd = input('是否继续充值,(Y or y)继续,(N or n)返回上一级: ')
                if readd == 'Y' or readd == 'y':
                    flag2 = False
                elif readd == 'N' or readd == 'n':
                    flag1 = False
                    flag2 = False
                else:
                    print('输入有误,请重新输入\n')
                    flag2 = True
        else:
            print('输入的不是数字请重新输入\n')


def usercenter(userlist):
    users = userlist
    print('=======用户中心=======\n 用户名: %s  余额: %s' % (users[0]['name'], users[0]['money']))


# 商品列表
def goods_list(goodslists):
    my_goods_list = goodslists
    print('请选择商品')
    for k, goods in enumerate(my_goods_list):
        print('产品编号:%s 名称: %s 价格: %s元, 剩余: %s' % (k, goods['name'], goods['price'], goods['nums']))
    while True:
        my_goods_id = input('请输入要买商品的编号: ')
        if my_goods_id.isdigit():
            return my_goods_id
        print('输入的不是数字请重新输入\n')


# 购物车模块
def cartlists(goodsid, cart_id):
    flag1 = True
    flag_list = True
    while flag1:  # 循环标记
        cart_id = cart_id + 1  # 为了给购物车里的商品编号,便于移除商品
        flag2 = True
        # goods_id = input('请输入要买商品的编号: ')
        goods_id = int(goodsid)
        print('您选择了 %s' % (goodslist[goods_id]['name']))
        goods_num = input('请输入购买数量: ')
        if goods_num.isdigit():
            print('您购买的数量是 %s 件' % (goods_num))
            goods_name = goodslist[goods_id]['name']
            goods_price = int(goodslist[goods_id]['price'])
            oldnum = int(goodslist[goods_id]['nums'])
            newnum = oldnum - int(goods_num)
            if newnum > 0:  # 判断产品数量够不够
                # goodslist[goods_id]['nums'] = newnum
                car_dict = {}  # 创建购物车字典
                car_dict['id'] = cart_id  # 编号
                car_dict['name'] = goods_name  # 商品名称
                car_dict['nums'] = goods_num  # 购买数量
                car_dict['price'] = goods_price * int(goods_num)  # 价格
                carlist.append(car_dict)
                while flag2:  # 循环标记
                    buy_again = input('是否继续购买请按(Y or y),去结账请按(N or n): ')
                    if buy_again == 'Y' or buy_again == 'y':
                        flag2 = False
                        flag1 = False
                        flag_list = True
                    elif buy_again == 'N' or buy_again == 'n':
                        flag1 = False
                        flag2 = False
                        flag_list = False
                    else:
                        print('输入有误,请重新输入\n')
                        flag2 = True
            else:
                print('没那么多了,看看还有没有别的想买的\n')
                break
        else:
            print('输入的不是数字请重新输入\n')
    # print('this is goods_num   ', goods_num)
    return carlist, flag_list, goods_id, newnum, cart_id


# 购买模块
def my_buy(cart_list, goods_id, goods_num):
    goods_total_money = 0
    if not cart_list:
        pass
    else:
        while True:
            print('购物车信息:')
            for buy_car in cart_list:
                print('购买编号: %s  产品名称: %s  价格: %s元  数量: %s个' % (
                    buy_car['id'], buy_car['name'], buy_car['price'], buy_car['nums']))
            my_confirm = input('订单确认请按(Y or y),移除请输入编号(数字),取消订单请按(N or n): ')
            if my_confirm == 'Y' or my_confirm == 'y':
                for cart in cart_list:
                    goods_total_money += cart['price']
                myoldmoney = int(users[0]['money'])
                # goodsmoney = carlist[goods_id]['price'] * int(goods_num)
                my_new_money = myoldmoney - goods_total_money
                if my_new_money >= 0:  # 判断钱够不够
                    users[0]['money'] = my_new_money
                    goodslist[goods_id]['nums'] = goods_num
                    print('提交成功!')
                    print('当前您的订单信息:')
                    for buy_car in car:
                        print('产品名称: %s  价格: %s元  数量: %s个' % (
                            buy_car['name'], buy_car['price'], buy_car['nums']))
                    # print('当前您的账户信息:\n', users)
                    for buy_users in users:
                        print('我的账户信息:\n账户名: %s  余额: %s元' % (buy_users['name'], buy_users['money']))
                    break
                else:
                    print('钱不够了请去充值,提交失败\n')
                    break
            elif my_confirm == 'N' or my_confirm == 'n':
                print('您已取消\n')
                break
            else:
                no_this_id = 0
                if my_confirm.isdigit():
                    # print(type(len(cart_list)))
                    for my_cart in cart_list:
                        if my_cart['id'] == int(my_confirm):
                            cart_list.remove(my_cart)
                            print('移除成功!')
                            no_this_id = 1
                            break
                elif no_this_id == 0:
                    print('没有这个编号,请重新输入\n')
                else:
                    print('输入有误,请重新输入\n')


if __name__ == '__main__':
    users = [
        {'name': 'wukun', 'money': 1000}
    ]
    goodslist = [
        {'name': '电脑', 'price': 1999, 'nums': 10},
        {'name': '鼠标', 'price': 10, 'nums': 10},
        {'name': '游艇', 'price': 20, 'nums': 10},
        {'name': '美女', 'price': 998, 'nums': 10}
    ]
    flag = True  # 最外层循环标记
    while flag:
        my_cart_id = -1
        flag_list = True
        carlist = []
        mainlist = '========我的网上商城=======\n1.进入商城\n2.账户充值'
        print(mainlist)
        choose = input('请选择编号(1 or 2),退出请按(E or e): ')
        if choose == 'E' or choose == 'e':
            break
        else:
            if choose.isdigit() and choose == '1':
                while flag_list:
                    goodsid = goods_list(goodslist)
                    car, flag_list, goods_id, goods_new_num, my_cart_id = cartlists(goodsid, my_cart_id)
                my_buy(car, goods_id, goods_new_num)
                while True:
                    my_choose = input('是否继续购物,确认请按(Y or y),退出请按(B or b): ')
                    if my_choose == 'Y' or my_choose == 'y':
                        flag = True
                        break
                    elif my_choose == 'B' or my_choose == 'b':
                        flag = False
                        break
                    else:
                        print('输入的信息不正确请重新输入')
                        continue

            elif choose.isdigit() and choose == '2':
                usercenter(users)
                recharge(users)
                print('this is users', users)
            else:
                print('输入的选项错误,请重新输入\n')

 

posted @ 2017-07-27 16:36  GodSpeed034  阅读(133)  评论(0编辑  收藏  举报