1. 程式人生 > >用戶認證登錄程序

用戶認證登錄程序

name 文件 word 只讀 輸出 登錄 失敗 break 狀態


# -*- coding:utf-8 -*-
# author: Wu changhao

#基礎需求:
#讓用戶輸入用戶名密碼
#認證成功後顯示歡迎信息
#輸錯三次後退出程序

count = 1 #計數器
number = 3 #剩余次數
while count <= 3: #循環3次錯誤後,退出程序。
username = input("please input your is username :")
password = input("please input your is password :")
number -= 1 #剩余次數每次減一次

if username == "changhao" and password == "null": #判斷正確的用戶名和密碼
print("登陸成功,歡迎您%s!" % username)
break #登陸成功後跳出while循環關閉程序
else:
if count <= 2: #判斷失敗允許登陸三次
print("登陸失敗,請重新輸入。剩余次數%s" % number)
elif count == 3: #判斷連續失敗三次,退出程序。
print("登陸失敗!剩余次數%s" % number)

break #剩余次數為0時,跳出while循環關閉程序
else:
print("登陸失敗!")
break
count += 1 #計數器每次循環累加一次


#升級需求:
#可以支持多個用戶登錄(提示,通過列表存多個賬戶信息)
#用戶3次認證失敗後,退出程序,再次啟動程序嘗試登陸時還是鎖定狀態

count = 1 #計數器
number = 3 #剩余次數
userdisc = {"xiaohong":0, "xiaoming":1, "xiaojun":2} #用字典生成用戶賬戶信息

file = open(‘lock_user.txt‘, ‘r‘) #打開鎖文件,設置為只讀模式
lock_file = file.readline() #將文件實例化文本文件
file.close() #關閉文件

while count <= 3: #循環3次錯誤後,退出程序。
number -= 1 # 剩余次數每次減一次
username = input("please input your is username :")
password = input("please input your is password :")
if username in lock_file: #如果賬戶名存在鎖文件內,則告知用戶被鎖定。
print("您好,%s賬戶已被鎖定!" % username)
break
elif username in userdisc and password == "null": #如果用戶名存在用戶字典內並且密碼匹配,則成功登陸。
print("您好%s,登錄成功!" % username)
break
else:
if count <= 2: #判斷失敗小於等於2次,則輸出。
print("登陸失敗,請重新輸入。剩余次數%s" % number)
elif count == 3: #判斷連續失敗三次,則執行。
file = open(‘lock_user.txt‘, ‘w‘) #打開鎖定文件,設置為寫模式。
if username in userdisc: #如果用戶存在用戶字典中,將用戶賬戶寫入鎖定文件。
file.write(username)
file.close()
print("登陸失敗!剩余次數%s" % number)
else:
print("登陸失敗!剩余次數%s" % number)
break #剩余次數為0時,跳出while循環關閉程序
else:
pass
count += 1 #計數器每次循環累加一次

用戶認證登錄程序