#!/usr/bin/env python3
# -*- coding:utf-8 -*-
#
# Author:   Payne Zheng <zzuai520@live.com>
# Date:     2018-04-11 10:41:15
# Location: DongGuang
# Desc:     User login authentication
#

def CheckUserStatus(user):
    """检查帐号是否在锁定文件中"""
    try:
        with open(user_lock_file, 'r', encoding="utf-8") as f1:for line in lines:
                if line.strip() == user:
                    return "lock"
    except FileNotFoundError:
        pass

"""设定变量"""
user_lock_file = "lock.txt"
user_retry_num = pass_retry_num = 3
user_pass_dict = {
    "jack": "abc123",
    "jushua": "123abc",
    "payne": "a1b2c3"
}

"""接收用户输入"""
input_user = input("\033[34mPlease enter your account number:\033[0m")
input_pass = input("\033[34mPlease enter your password:\033[0m")

"""检查用户帐号状态,如在锁定帐号文件则报错退出"""
if CheckUserStatus(input_user) == "lock":
    print(
        """\033[31mError! %s user has been locked,"""
        """unable to login, please contact customer service phone 10086.\033[0m""")
    exit()

"""验证帐号密码"""
while True:
    # 帐号密码正确,打印欢迎信息
    if input_user in user_pass_dict and user_pass_dict.get(input_user) == input_pass:
        print("\033[32mSuccessful login, welcome <%s>\033[0m" % input_user)
        break
    else:
        # 帐号错误,提示帐号错误信息
        if input_user not in user_pass_dict:
            print("\033[33mSrror! the user <%s> in not exist" % input_user)
            input_user = input("please re-enter (you still have %s retry opportunity): \033[0m""" % (user_retry_num - 1))
            user_retry_num -= 1
            if user_retry_num == 1:
                print("\033[31mSorry, you enterd accounts <%s> not exist exceeds the number of retries" % input_user)
                break
        # 密码错误,提示密码错误信息
        else:
            print("\033[33mSrror! the password entered is not correct")
            input_pass = input("please re-enter (you still have %s retry opportunity): \033[0m"% (pass_retry_num-1))
            pass_retry_num -= 1
            # 重输密码三次后打印帐号锁定信息,并将帐号存入锁定文件进行锁定
            if pass_retry_num == 1:
                print(
                    """\033[31mSorry, the accounts <%s> password input error exceeds the number of retries"""
                    """the account has been locked!\033[0m""" % input_user)
                with open(user_lock_file, 'a', encoding="utf-8") as f:
                    f.writelines(input_user + "\n")
                break

 

posted on 2018-04-11 14:57  PAYNE1Z  阅读(318)  评论(0)    收藏  举报