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

    product_list = [['Iphone7', 5800],
                    ['Coffee', 30],
                    ['疙瘩汤', 10],
                    ['Python Book', 99],
                    ['Bike', 199],
                    ['ViVo X9', 2499],
    
                    ]
    # 定义一个空字典用于存放商品
    shopping_cart = {}
    # 定义一个空列表用于存放用户信息
    current_userinfo = []
    db_file = r'db.txt'
    while True:
        print('''
        1 登录
        2 注册
        3 购物
        ''')
        shopping = input("请选择你的操作:")
        if shopping == '1':
            over = True
            count = 0
            while over:
                if count == 3:
                    print("错误次数过多退出")
                    break
                username = input("请输入用户名:")
                userpasswd = input("请输入密码:")
                with open(db_file, 'r', encoding='utf-8') as user:
                    for line in user:
                        line = line.strip('
    ')
                        user_info = line.split(',')
                        username_of_db = user_info[0]
                        pwd_of_db = user_info[1]
                        balance_of_db = int(user_info[2])
                        if username == username_of_db and userpasswd == pwd_of_db:
                            print("登录成功")
                            # 将用户名字和余额存入列表中
                            current_userinfo = [username_of_db, balance_of_db]
                            print('用户信息为:', current_userinfo)
                            over = False
                            break
                    else:
                        print('用户或密码错误')
                        count += 1
        elif shopping == '2': #开始注册你的用户
            username = input("请输入你的用户名:")
            while True:
                pwd1 = input("请输入你的密码:")
                pwd2 = input("请在此输入密码:")
                if pwd1 != pwd2:
                    print('两次输入的不一致,请重新输入')
                else:
                    break
            money = input("请输入你的余额:")
            with open(db_file, 'a', encoding='utf-8') as f:
                f.write('%s,%s,%s
    ' % (username, pwd1, money))
        elif shopping == '3': #开始选购
            if len(current_userinfo)==0:
                print("请登录。。。。。")
            else:
                print("登录成功,开始购物")
                username_of_db=current_userinfo[0]
                balance_of_db=current_userinfo[1]
                print('尊敬的用户%s您的余额为%s祝您购物愉快'%(username_of_db,balance_of_db))
                over=True
                while over:
                    for index,product in enumerate(product_list):
                        print(index,product)
                    shopping=input("请输入商品编号,输入’q‘ 退出: ")
                    if shopping.isdigit():
                        shopping=int(shopping)
                        if shopping <0 or shopping>= len(product_list):continue
                        pname=product_list[shopping][0]
                        pprice=product_list[shopping][1]
                        if balance_of_db >pprice:
                            if pname in shopping_cart: #判断商品是否已经在购物车中
                                shopping_cart[pname]['count']+=1
                            else:
                                shopping_cart[pname]={'pprice':pprice,'count':1}
                                balance_of_db-=pprice # 扣钱
                                current_userinfo[1]=balance_of_db #更新用户余额
                                print("Added product" + pname + " into shopping_cart,33[42;1myour current33[0m balance " + str(balance_of_db))
                        else:
                            print("余额不足,商品总额是{price},你还差{lack_price}".format(price=pprice,lack_price=(pprice-balance_of_db)))
                        print(shopping_cart)
                    elif shopping =='q':
                        print('''
                        ---------------------------------已购买商品列表---------------------------------
                        id          商品                   数量             单价               总价
                        ''')
                        total_cost=0
                        for y,key in enumerate(shopping_cart):
                            print('%22s%18s%18s%18s%18s' %(
                                y,
                                key,
                                shopping_cart[key]['count'],
                                shopping_cart[key]['pprice'],
                                shopping_cart[key]['pprice'] * shopping_cart[key]['count']
                            ))
                            total_cost+=shopping_cart[key]['pprice'] * shopping_cart[key]['count']
                        print("""
                        您的总花费为:%s
                        您的余额为:%s
                        """ %(total_cost,balance_of_db))
                        while over:
                            inp=input("确认购买(yes/no): ")
                            if inp not in ['Y','y','N','n','yes','no']:continue
                            if inp in ['Y','y','yes']:
                                src_file=db_file
                                dst_filer=r'%s.swap' %db_file
                                with open(src_file,'r',encoding='uft-8') as read_f,
                                    open(dst_filer,'w',encoding='utf-8') as write_f:
                                    for line in read_f:
                                        if line.startswith(username_of_db):
                                            l=line.strip('
    ').split(',')
                                            l[-1]=str(balance_of_db)
                                            line=','.join(1)+'
    '
                                        write_f.write(line)
                                os.remove(src_file)
                                os.rename(dst_filer,src_file)
                                print('购买成功,请耐心发货')
                            shopping_cart={}
                            current_userinfo={}
                            over=False
                    else:
                        print('非法操作')
        else:
            print('非法输入')
  • 相关阅读:
    使用SeaJS实现模块化JavaScript开发
    使用antixss防御xss
    AntiXSS
    FFmpeg 安装、日常使用及应用场景记录与总结。
    Cygwin 安装、设置及配置 FaTTY 多标签页(Tabs)
    Python 爬取B站(Bilibili.com)UP主的所有公开视频链接及信息
    windows10 彻底删除蓝牙设备,蓝牙设备删除失败解决方案
    VSCode 常用设置、快捷键及插件
    Python 文件IO:JSON 文件的读取与写入
    Python 文件IO:TXT 文件的读取与写入
  • 原文地址:https://www.cnblogs.com/yjc53/p/13378658.html
Copyright © 2011-2022 走看看