文件操作作业

需求:
1. 实现用户注册登陆功能
2. 用户密码加密
3. 用户信息持久化保存

代码

#!/usr/bin/env python
# -*- utf-8 -*-

'''
    需求:
        1. 实现用户注册登陆功能
        2. 用户密码加密
        3. 用户信息持久化保存

'''
# 导入模块
import hashlib    # 加密
import pickle    # 持久化
import os

# 数据文件
user_db = 'user.db'

# md5加密密码
def md5_encrypt(data):
    hash = hashlib.md5()
    hash.update(data.encode('utf-8'))
    return hash

# 创建数据文件
f = open(user_db,'a',encoding='utf-8')
f.close()

# 循环开关
tag = True
while tag:
    print('''
        1 注册
        2 登陆
        3 退出
    ''')

    choice = input('请输入你的操作: ').strip()

    # 注册
    if choice == '1':
        count = 0
        # 控制只能输错三次
        while count < 3:
            user = input('请输入用户名: ').strip()
            passwd = input('请输入密码: ').strip()
            passwd2 = input('请再次输入密码: ').strip()

            # 判断文件不为空
            if os.path.getsize(user_db) > 0:
                with open(user_db,'rb') as read_f:
                    load_dic = pickle.load(read_f)
                    if user in load_dic:
                        print('用户已经存在了,请登录')
                        break

            # 两次密码相同
            if passwd == passwd2:
                user_dic = {}
                # 密码加密
                passwd = md5_encrypt(passwd).hexdigest()
                # 将用户信息加入字典
                user_dic[user] = passwd
                with open(user_db,'rb') as read_f:
                    if os.path.getsize(user_db) > 0:
                        # 反序列化文件取值
                        load_dic = pickle.load(read_f)
                        # 将用户信息字典更新到数据文件取出来的值
                        load_dic.update(user_dic)
                        with open(user_db,'wb') as write_f:
                            # 写入文件
                            pickle.dump(load_dic,write_f)
                    else:
                        with open(user_db,'ab') as write_f:
                            pickle.dump(user_dic,write_f)
                    break
            else:
                count += 1
                continue
        else:
            print('都输错三次了,别注册了')
            tag = False
    # 登陆
    elif choice == '2':
        count =0
        while count < 3:
            # 登陆用户密码
            user = input('请输入用户名: ').strip()
            passwd = input('请输入密码: ').strip()
            # 密码加密
            passwd = md5_encrypt(passwd).hexdigest()
            with open(user_db,'rb') as read_f:
                # 反序列化读取数据文件
                load_dic = pickle.load(read_f)
                if user in load_dic:
                    if load_dic[user] == passwd:
                        print('登陆成功')
                        # 进入命令行
                        while True:
                            cmd = input('>>: ').strip()
                            if cmd == 'q' or cmd == 'Q':
                                exit('拜拜')
                            elif cmd:
                                print('你输入的命令是 [%s]' % cmd)
                            else:
                                print('不允许输入空')
                                continue
                    # 密码输入错误
                    else:
                        print('密码错误')
                        count += 1
                        continue
                else:
                    print('用户不存在,请注册')
                    break
    # 退出
    elif choice == '3':
        tag = False
    else:
        print('非法输入')
        continue
View Code

 效果图

需求:
1. 实现魔镜功能,提问问题他知道就回答
2. 如果不知道就告诉该怎么回答
3. 可以永久记住教的答案

代码

#!/usr/bin/env /python
# -*- coding: utf-8 -*-

'''
    需求:
        1. 实现魔镜功能,提问问题他知道就回答
        2. 如果不知道就告诉该怎么回答
        3. 可以永久记住教的答案
'''

import pickle
import os
answer_data = 'answer.db'

# 如果文件不存在则创建
if not os.path.exists(answer_data):
    open(answer_data,'w')

while True:
    answer_dic = {}
    question = input('您好,我是魔镜,您要问什么问题? ').strip()
    if not question:
        print('不允许输入空的问题')
        continue
    elif question == 'q' or question == 'Q':
        break

    if os.path.getsize(answer_data) <= 0:
        answer = input('您是第一个问魔镜的人,要告诉我问题的答案: ').strip()
        if not answer:
            print('不允许输入空的答案')
            continue
        with open(answer_data,'ab') as write_f:
            answer_dic[question] = answer
            pickle.dump(answer_dic,write_f)
    else:
        with open(answer_data,'rb') as read_f:
            load_answer = pickle.load(read_f)
            if question in load_answer:
                print(load_answer[question])
            else:
                answer = input('快告诉我是什么答案吧: ').strip()
                if not answer:
                    print('不允许输入空的答案')
                    continue
                answer_dic[question] = answer
                load_answer.update(answer_dic)
                with open(answer_data,'wb') as write_f:
                    pickle.dump(load_answer,write_f)
View Code

效果图

posted @ 2018-11-03 23:08  浩小白  Views(137)  Comments(0Edit  收藏  举报