day 09

1、编写文件copy工具

with open('a.txt', 'r', encoding='utf-8')as f1,\
    open('b.txt', 'w', encoding='utf-8')as f2:
    res1 = f1.read()
    f2.write(res1)

2、编写登录程序,账号密码来自于文件

user_name = input('请输入用户名:').strip()
user_pwd = input('请输入密码:').strip()
with open('userinfo.txt', 'r', encoding='utf-8')as f:
    for info in f:
        username, password = info.strip().split(':')
        if user_name == username and user_pwd == password:
            print('登录成功!')
            break
    else:
        print('用户名或者密码错误!')

3、编写注册程序,账号密码来存入文件

user_name = input('请输入用户名:').strip()
user_pwd = input('请输入密码:').strip()
with open('register_info.txt', 'r', encoding='utf-8')as f1, \
        open('register_info.txt', 'a', encoding='utf-8')as f2:
    for i in f1:
        if i:
            username, password = i.strip().split(':')
            if user_name == username:
                print('该用户已经存在')
    else:
        f2.write('{}:{}\n'.format(user_name, user_pwd))

二:

2.1:编写用户登录接口

1、输入账号密码完成验证,验证通过后输出"登录成功"

2、可以登录不同的用户

3、同一账号输错三次锁定,(提示:锁定的用户存入文件中,这样才能保证程序关闭后,该用户仍然被锁定)

2.2:编写程序实现用户注册后,可以登录,

#登录功能
def login():
    count = 0
    set1 = set()
    list1 = []
    tag = True
    while tag:
        user_name = input('请输入用户名:').strip()
        user_pwd = input('请输入密码:').strip()
        with open('userinfo.txt', 'r', encoding='utf-8')as f:
            for i in f:
                if i:
                    username, password,locking = i.strip().split(':')
                    set1.add(username)
                    #判断用户是否注册
                    if user_name in set1:
                        # 判断用户是否锁定
                        if locking == 'locked':
                            print('该用用户已经被锁定!')
                            tag = False
                            break
                        else:
                            if user_name == username and user_pwd == password:
                                print('登录成功!')
                                tag = False
                                break

            else:
                print('用户名或密码错误!')
                if user_name in set1:
                    count += 1
                    if count == 3:
                        #输错三次后,修改登录用户的锁定状态
                        print('输错三次,该用户已被锁定')
                        user_locked = f'{username}:{password}:locked\n'
                        with open('userinfo.txt', 'r', encoding='utf-8')as f1:
                            for i in f1:
                                # print(i)
                                #记录其他用户信息,将其他用户放入列表中
                                if user_name != i.strip().split(":")[0]:
                                    list1.append(i)
                            #添加登录用户状态到列表中
                            list1.append(user_locked)
                        with open('userinfo.txt', 'w', encoding='utf-8')as f2:
                            for l in list1:
                                #将用户信息重新写入文件中
                                f2.write(l)
                            tag = False

#注册功能
def register():
    while True:
        user_name = input('请输入用户名:').strip()
        if user_name == 'q':
            break
        user_pwd = input('请输入密码:').strip()
        with open('userinfo.txt', 'r', encoding='utf-8')as f1, \
                open('userinfo.txt', 'a', encoding='utf-8')as f2:
            for user in f1:
                if user:
                    username = user.strip().split(':')[0]
                    if username == user_name:
                        print('该用户已存在!')
                        break
            else:
                f2.write(f'{user_name}:{user_pwd}:not_lock\n')
                print('注册成功!')
                break

#功能函数
func_dic = {
    '1': login,
    '2': register
}

while True:
    print('''
    =====================
    0 退出
    1 登录
    2 注册
    =====================
    ''')
    cmd = input('请输入命令编号>>:').strip()
    if not cmd.isdigit():
        print('请输入数字')
        continue
    elif int(cmd) == 0:
        break
    elif cmd not in func_dic:
        print('请输入正确的编号')
        continue
    else:
        func_dic.get(cmd)()
posted @ 2020-03-13 22:24  蛋蛋的丶夜  阅读(294)  评论(0编辑  收藏  举报