python-day1作业记录(用户登陆系统,3次输入错误锁定用户)

# -*- coding:utf-8 -*-
__author__ = 'admin'
'''
locked.txt:
liuxiaoyu
xiaodong
tenglan
match.txt:
alex 123456
xiaoyu 6543210
wulongyuan 6667770
'''
import sys

account_file = 'c:\python作业\day1\match.txt'
locked_file = 'c:\python作业\day1\locked.txt'

def deny_account(username):
    print('您的用户已被锁定!')
    with open(locked_file, 'a') as deny_f:
        deny_f.write('\n' + username)

def main():
    retry_count = 0
    retry_limit = 3
    while retry_count < retry_limit:
        username = input('\033[32;1m请输入用户名:\033[0m')
        if len(username) == 0:
            print('用户名不能为空,请重新输入!')
            continue
        with open(locked_file, 'r') as lock_f:
            for line in lock_f.readlines():
                if username == line.strip():
                    sys.exit('\033[32;1m用户 %s 已经被锁定!\033[0m' % username)


        while True:
            password = input('\033[32;1m请输入密码:\033[0m')
            if len(password) == 0:
                print('密码不能为空,请重新输入!')
                continue
            else:
                break
        with open(account_file, 'r') as account_f:
            flag = False
            for line in account_f.readlines():
                if len(line.strip()) == 0:
                    continue
                user, pawd = line.strip().split()
                if username == user and password == pawd:
                    print('success!')
                    flag = True
                    break
        if flag == False:
            if retry_count < 2:
                print('您的用户名或密码有误,请重新输入!')
            retry_count += 1
        else:
            print('欢迎用户%s登陆老男孩系统!' % username)
            break
    else:
        deny_account(username)

if __name__ == '__main__':
    main()

  

猜你喜欢

转载自www.cnblogs.com/laotieshan/p/12063356.html
今日推荐