zoukankan      html  css  js  c++  java
  • 简单购物车

    数据结构:
    goods = [
    {"name": "电脑", "price": 1999},
    {"name": "鼠标", "price": 10},
    {"name": "游艇", "price": 20},
    {"name": "美女", "price": 998},
    ......
    ]
    
    功能要求:
    基础要求:
    
    1、启动程序后,输入用户名密码后,让用户输入工资,然后打印商品列表
    
    2、允许用户根据商品编号购买商品
    
    3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
    
    4、可随时退出,退出时,打印已购买商品和余额
    
    5、在用户使用过程中, 关键输出,如余额,商品已加入购物车等消息,需高亮显示
    
    
    扩展需求:
    
    1、用户下一次登录后,输入用户名密码,直接回到上次的状态,即上次消费的余额什么的还是那些,再次登录可继续购买
    
    2、允许查询之前的消费记录
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # @Date    : 2018-04-29 12:16:23
    # @Author  : hjc (284738209@qq.com)
    # @Link    : http://www.cnblogs.com/tootooman/
    # @Version : 2
    
    
    import os
    import re
    import time
    import json
    
    goods = [
        {"name": "电脑", "price": 1999},
        {"name": "鼠标", "price": 10},
        {"name": "游艇", "price": 20},
        {"name": "美女", "price": 998},
    ]
    
    userdb = 'userdb'
    if not os.path.isfile(userdb):
        db = open(userdb, 'w', encoding='utf8')
        db.write('{}')
        db.close()
    
    shopping_car = {}
    
    
    flag = True
    
    while flag:
        username = input('用户名:').strip()
        password = input('密码:').strip()
        with open(userdb, 'r+') as users:
            users_data = json.load(users)
            if username in users_data:
                if password == users_data[username]:
                    # os.path.isfile(username)
                    check = input('是否要查看消费历史[y:查看/任意键:进入商城]:').strip()
                    f = open(username, 'r', encoding='utf8')
                    if check == 'y':
                        print('start'.center(60, '-'))
                        for line in f:
                            print(line)
                        print('end'.center(60, '-'))
    
                    ret = ''
                    f.seek(0)
                    for line in f:
                        ret = re.findall('余额.*', line)
                    salary = int(ret[0].split('')[-1])
    
                    f.close()
                    break
                else:
                    print('用户名或密码错误。')
    
            else:
                print('你不是会员!系统已自动帮你注册会员!')
                users.seek(0)
                users_data[username] = password
                json.dump(users_data, users)
                while 1:
                    salary = input('兜里有多少钱::').strip()
                    if salary.isdigit():
                        salary = int(salary)
                        f = open(username, 'w', encoding='utf8')
                        f.close()
                        flag = False
                        break
                    else:
                        print('你兜里的不是钱!')
    
    while 1:
        title = '''
        %s
        %s
        %s
        ''' % (''.center(60, '*'), '商品列表'.center(60, ' '), ''.center(60, '*'))
        print(title)
        for i in range(len(goods)):
            info = '''
            商品编号:%s    商品名称:%s    商品价格:%s
            ''' % (i, goods[i]['name'], goods[i]['price'])
            print(info)
        choice = input('输入商品编号[q:结算]:').strip()
        if choice.isdigit():
            choice = int(choice)
            if choice >= 0 and choice < len(goods):
                if salary >= goods[choice]['price']:
                    goods_name = goods[choice]['name']
                    goods_price = goods[choice]['price']
                    salary -= goods_price
                    if shopping_car.get(goods_name):
                        shopping_car[goods_name]['total'] += 1
                        print('33[1;32m%s33[0m已放到购物车,购买后兜里还剩 33[1;31m%s33[0m' % (
                            goods_name, salary))
                    else:
                        shopping_car[goods_name] = {}
                        shopping_car[goods_name]['price'] = goods_price
                        shopping_car[goods_name]['total'] = 1
                        print('33[1;32m%s33[0m已放到购物车,购买后兜里还剩 33[1;31m%s33[0m' % (
                            goods_name, salary))
                else:
                    print('兜里钱不够!兜里还剩 33[1;31m%s33[0m !' % (salary,))
                    while 1:
                        get_money = input('要不要借一点?[n:不要]:').strip()
                        if get_money.isdigit():
                            get_money = int(get_money)
                            salary += get_money
                            print('借 钱 成 功!当前余额为:%s' % (salary,))
                            break
                        elif get_money == 'n':
                            break
                        else:
                            print('信不信我揍你。。。')
            else:
                print('没有相关商品。')
        elif choice == 'q':
            if len(shopping_car) != 0:
                pay_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
                print('购物车信息:买完兜里还剩 33[1;31m%s33[0m' % (salary))
                with open(username, 'a+', encoding='utf8') as f:
                    f.write('%s
    ' % (pay_time.center(50, '=')))
                    for item in shopping_car:
                        shopping_list = '''
                        商品名称:%s    商品价格:%s    已买数量:%s    合计:%s
                        ''' % (item,
                               shopping_car[item]['price'],
                               shopping_car[item]['total'],
                               shopping_car[item]['total'] *
                               shopping_car[item]['price'],
                               )
                        print(shopping_list.strip())
                        f.write('%s
    ' % (shopping_list.strip()))
                    f.write('余额:%s
    ' % (salary))
                break
            else:
                print('没有买任何东西。')
                break
    View Code
  • 相关阅读:
    rmse均方根误差
    小波变换
    时频工具箱-gabor
    时频工具箱tftb
    Gabor变换
    计算非零元素
    用“人话”解释不精确线搜索中的Armijo-Goldstein准则及Wolfe-Powell准则
    步长与学习率
    chentos7 安装jdk1.8(二进制安装)
    端口转发之 nc
  • 原文地址:https://www.cnblogs.com/tootooman/p/8975637.html
Copyright © 2011-2022 走看看