用户登录验证(新增帐号错误重试)
生活随笔
收集整理的這篇文章主要介紹了
用户登录验证(新增帐号错误重试)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#!/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)breakelse:# 帳號錯誤,提示帳號錯誤信息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 -= 1if 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
?
轉載于:https://www.cnblogs.com/PAYNE1Z/p/8795772.html
總結
以上是生活随笔為你收集整理的用户登录验证(新增帐号错误重试)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c#使用Stopwatch来计算时间间隔
- 下一篇: 1.1 sql注入分类与详解