Python练习--购物车

#!/usr/bin/evn python
# -*- coding:utf-8 -*-
# author:stealth

import os
product_list = [['Iphone8',6000],
                ['Coffee',30],
                ['Python Book',99],
                ['Bike',1999],
                ['TV',2999]

]

shoping_cart = {}
current_userinfo = []
db_file='db.txt'

while True:
    print('''
    1 登陆
    2 注册
    3 购物
    ''')
    choice = input('请输入序号>>:').strip()
    if choice == '1':
        tag = True
        count = 0
        while tag:
            if count == 3:
                print('\033[45m尝试次数过多,退出。。。\033[0m')
                break
            uname = input('请输入用户名:').strip()
            pwd = input('请输入密码:').strip()

            with open(db_file,'r',encoding='utf-8') as f:
                for line in f:
                    line = line.strip('\n')
                    user_info = line.split('|')
                    uname_of_db = user_info[0]
                    pwd_of_db = user_info[1]
                    balance_of_db = int(user_info[2])

                    if uname == uname_of_db and pwd == pwd_of_db:
                        print('登陆成功')
                        current_userinfo=[uname_of_db,balance_of_db]
                        print('用户信息为:',current_userinfo)
                        tag = False
                        break
                else:
                    print('用户名或密码错误')
                    count += 1

    elif choice == '2':
        uname = input('请输入用户名:').strip()
        with open(db_file, 'r', encoding='utf-8') as f:
            for line in f:
                line = line.strip('\n')
                user_info = line.split('|')
                uname_of_db = user_info[0]
                if uname == uname_of_db:
                    print('对不起,您已经注册过了!')
                    tag = False
                    break
                else:
                    tag =True
        # tag = False
        while tag:
            pwd1 = input('请输入密码:').strip()
            pwd2 = input('请再次输入密码:').strip()
            if pwd1 == pwd2:
                balance = input('请输入充值金额:').strip()
                with open(db_file, 'a', encoding='utf-8') as f:
                    f.write('%s|%s|%s\n' % (uname, pwd1, balance))
                print('恭喜[%s]注册成功' % uname)
                break
            else:
                print('两次输入密码不一致,请重新输入!')

    elif choice == '3':
        if len(current_userinfo) == 0:
            print('请先登陆!')
        else:
            uname_of_db = current_userinfo[0]
            balance_of_db = current_userinfo[1]
            print('尊敬的用户[%s],您的余额为:[%s],祝您购物愉快' %(uname_of_db,balance_of_db))

            tag = True
            while tag:
                for index,product in enumerate(product_list):
                    print(index,product)
                choice = input('请输入商品编号购物或输入q退出>>').strip()
                if choice.isdigit():
                    choice = int(choice)
                    if choice < 0 or choice >= len(product_list):continue
                    pname = product_list[choice][0]
                    pprice = product_list[choice][1]
                    if balance_of_db > pprice:
                        if pname in shoping_cart:
                            shoping_cart[pname]['count'] += 1
                        else:
                            shoping_cart[pname] = {'pprice':pprice,'count':1}
                        balance_of_db -= pprice
                        current_userinfo[1] = balance_of_db
                        print("Added product " + pname + " into shopping cart,myour current balance " + str(balance_of_db))
                    else:
                        print('对不起,您的余额不足!产品价格是{price},你还差{lack_price}'.format(price=pprice,lack_price=(pprice-balance_of_db)))
                    print(shoping_cart)
                elif choice == 'q':
                    print('''
                    ---------------------------------已购买商品列表---------------------------------
                    id          商品                   数量             单价               总价
                    ''')
                    total_cost = 0
                    for i,k in enumerate(shoping_cart):
                        print('%22s%18s%18s%18s%18s' %(i,k,shoping_cart[k]['count'],shoping_cart[k]['pprice'],shoping_cart[k]['pprice']*shoping_cart[k]['count']))
                        total_cost += shoping_cart[k]['pprice'] * shoping_cart[k]['count']

                    print('''
                    您的总花费为:%s
                    您的余额为:%s
                    ---------------------------------end---------------------------------
                    ''' %(total_cost,balance_of_db))

                    while tag:
                        inp = input('请确认购买(yes/no)>>:').strip()
                        if inp not in ['Y','N','y','n','yes','no']:continue
                        if inp in ['Y','y','yes']:
                            src_file = db_file
                            dst_file = 'newdb.txt'
                            with open(src_file,'r',encoding='utf-8') as read_f,\
                                open(dst_file,'w',encoding='utf_8') as write_f:
                                for line in read_f:
                                    if line.startswith(uname_of_db):
                                        l = line.strip('\n').split('|')
                                        l[-1]=str(balance_of_db)
                                        line='|'.join(l)+'\n'
                                    write_f.write(line)
                            os.remove(src_file)
                            os.rename(dst_file,src_file)
                            print('购买成功,请耐心等待发货!')
                        shoping_cart = {}
                        current_userinfo = []
                        tag = False

                    else:
                        print('您输入的有误,请重新输入!')
    else:
        print('您输入的有误,请重新输入!')

 

你可能感兴趣的:(python练习)