某宝购物系统

user_list = []
user_login = {}
shopping_car = []


def read_all_user():
    # 避免重复读取
    if user_list:
        return
    # 读取文件内容转成用户列表
    with open('account_info.txt', 'rt', encoding='utf-8') as f:
        text = f.read()
        if not text: return
        # 将读到的内容转换成 字典
        texts = text.split(',')
        for i in texts:
            # 将每个用户数据切成列表
            temp = i.split('|')
            print(temp)
            d_dic = {}
            d_dic['name'] = temp[0]
            d_dic['pwd'] = temp[1]
            d_dic['money'] = temp[2]
            # 把转换好的用户信息存到列表中
            user_list.append(d_dic)


def user_exist(username):
    for i in user_list:
        if i['name'] == username:
            return True
        else:
            return False


# 将用户信息写入到文件里
def write_user(name, password):
    with open('account_info.txt', 'a+', encoding='utf-8') as f:
        # 写入
        old = ' %s|%s|0' % (name, password)
        f.write(old)


def register():
    while True:
        name = input('请输入用户名:').strip()
        password = input('请输入密码:').strip()
        # 如果两个输入都不为空
        if name and password:
            print('OK')
            # 判断是否存在
            if user_exist(name):
                print('用户名已存在')
                continue
            else:
                print('可以注册')
                # 密码是否少于六位
                if len(password) < 6:
                    print('密码不能少于6位')
                    continue
                else:
                    print('注册成功')
                    write_user(name, password)
                    # 因为你读取文件是在程序一开始执行时读取的 所以新注册的用户会无法登录
                    # 手动添加新用户
                    user_list.append({'name': name, 'pwd': password, 'money': 0})
                    return
        else:
            print('用户名或密码不能为空')

#
# register()


def add_money():  # 充值
    if not user_login:
        print('请先注册或登陆')
        return
    else:
        add_money = int(input('要充值的金额'))
        user_login['money'] = int(user_login['money']) + add_money
        print('账户余额%s' % user_login['money'])


import time


# 锁定某个账户
def lock_user(username):
    for user in user_list:
        if user['name'] == username:
            # 写入时间
            user['locktime'] = str(time.time())
            print(username + '被锁定')
            print(user_list)
            # 同步数据到文件
            # write_users_to_file()


# 购物
def shopping():
    if not user_login:
        print('请先注册或登陆')
        return
    while True:
        shoping = [['1', '口红', 300], ['2', '棉袄', 800], ['3', '棉裤', 200]]
        for i in shoping:
            print('商品号:{},{}:{}'.format(i[0], i[1], i[2]))
        num = input('请输入您要购买的商品序号或者物品名(输入0退出):')
        if num == '0':
            print(shopping_car)
            return
        for i in shoping:
            if num not in i:
                # print('请输入正确的序号或者物品名称')
                continue
            else:
                user_login['shopping_car'].append(i)


def show_shopping_car():  # 购物车
    if not user_login:
        print('请先注册或登陆')
        return
    print(user_login['shopping_car'])

def account_info():
    if not user_login:
        print('请先注册或者登陆')
        return
    print('账号:{}  \n密码:{}  '.format(user_login['name'], user_login['pwd']))


# 用于保存错误次数
error_dic = {}


# 登录功能
def login():
    while True:
        name = input('输入用户名 输入0返回上层:')
        if name == '0':
            return
        # 查看是否有这个用户 如果没有就直接重新输入
        flag = False
        for user in user_list:
            if user['name'] == name:
                flag = True
                break
        else:
            print('用户不存在,请先注册')
            continue
        password = input('输入密码')
        # 文件内容以及读取到列表中
        for user in user_list:
            if user['name'] == name and user['pwd'] == password:
                print('登录成功 欢迎: %s ' % name)
                user_login['name'] = name
                user_login['pwd'] = password
                user_login['money'] = user['money']
                user_login['shopping_car'] = shopping_car
                return True
        print('用户名或密码不正确 请重新输入')
        # 存入错误次数
        if name not in error_dic:
            error_dic[name] = 1
        else:
            error_dic[name] = 1 + error_dic[name]

            # num =error_dic[name]
            # print('剩余尝试次数 %d' %(3- num))
            # #已经错误三次
            # if num ==3:
            #     lock_user(name)


# 定义一个自定保存所有的函数
method_dic = {'1': register,
              '2': login,
              '3': shopping,
              '4': show_shopping_car,
              '5': account_info,
              '6': add_money}


def main():
    # 加载用户数据
    read_all_user()

    while True:
        print('''
欢迎使用x宝商城 请选择
1:注册
2:登录
3:购物
4:查看购物车
5:账户信息
6:账户充值
0:退出系统
        ''')
        # 接收用户选择的序号
        choice = input('请输入执行的功能:').strip()
        # 如果输入的是0 则退出方法
        if choice == '0':
            return
        if choice not in method_dic:
            print('输入有误 请重新输入')
            continue
        # 如果上面都没有返回说明 输入的序号没问题
        # 根据输入从字典中取出方法并执行
        method_dic[choice]()

main()
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值