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('非法输入')
  • 相关阅读:
    C# @符号的多种使用方法
    C#抽象工厂简单实现类
    项目总结——谈谈封装(抽象工厂+反射+缓存机制)
    C#中String类的几个方法(IndexOf、LastIndexOf、Substring)
    String str 与 String str=new String("") 区别
    sql面试题
    [bzoj2038]莫队算法学习
    分治算法初步
    线段树训练
    [poj2104]可持久化线段树入门题(主席树)
  • 原文地址:https://www.cnblogs.com/yjc53/p/13378658.html
Copyright © 2011-2022 走看看