Python用户登录验证用户名3次

本文实现目标为:在用户进行登录时,首先验证用户输入的用户名是否在没名单内,如果在,输出用户名在黑名单的提示信息。否则就判断用户名和密码是否属于提前设置好的,如果是,就输出欢迎登录提示信息,否则输出密码错误提示信息。当用户名输入3次都不能和设置好的用户名和密码匹配的话,就锁定该用户,并加入黑名单。

#coding=utf-8

import os

username = 'wangph'
password = 'qazplm'
count = 0

# file = os.path.exists('user.txt')
# if file == True:
blacklists = open('user.txt', 'r').readlines()

while True:
    input_user = raw_input('Please input username: ')
    input_pass = raw_input('Please input password: ')
    count += 1
    for blacklist in blacklists:
        if blacklist == input_user:
            print('The user name has been disabled')
            break
    if username == input_user and password == input_pass:
        print('Welcome login...')
    elif count > 0 and count < 3:
        print('ERROR Incorrect username or password!!!')
        continue
    else:
        print('The user name has been disabled')
        blacklists = open('user.txt', 'a+')
        blacklists.write('%s' % input_user)
        blacklists.close()
View Code

存在的问题:

1、读取文本中的黑名单时,关于换行等操作并不熟练。

2、在验证是否在黑名单之后,如果在,跳出循环之后,还会经历验证用户名和密码的过程。

3、写入黑名单时,如何换行

参考博客:https://www.cnblogs.com/smelond/p/7828020.html

猜你喜欢

转载自www.cnblogs.com/phw110/p/9199072.html
今日推荐