小編這次要給大家分享的是Python如何實現(xiàn)文件操作模擬用戶登陸,文章內(nèi)容豐富,感興趣的小伙伴可以來了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。
我們提供的服務(wù)有:成都網(wǎng)站建設(shè)、做網(wǎng)站、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認證、洛浦ssl等。為上千余家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的洛浦網(wǎng)站制作公司題目要求
1、輸入用戶名和密碼后回車
2、密碼輸入錯誤,給出提示,并選擇是否重新輸入
3、密碼輸入錯誤三次后,用戶被鎖定,無法繼續(xù)登陸
構(gòu)思
1、用戶輸入賬號和密碼后,需要判斷賬號是否存在
2、判斷賬號是否被禁用(錯誤次數(shù)大于三次)
3、判斷賬號密碼是否正確
4、不同的錯誤給出不同的提示
5、每輸入錯一次,文檔中的錯誤次數(shù)需要更新
6、如果三次以內(nèi)用戶登陸成功,密碼原來的錯誤次數(shù)被重置
題目完成步驟
1、文檔的編寫
考慮到數(shù)據(jù)的存儲問題,決定將賬號、密碼、錯誤次數(shù)進行分行存儲,三行為一組用戶信息
2、代碼編寫
go = True while go: # 用來判斷賬號是否存在 no_existence_flag = True # 用來判斷是否輸入正確 no_flag = True # 用來判斷是否已經(jīng)被封 disable_flag = True # 用來判斷次數(shù)是否已經(jīng)超過限制 account = input("account:") password = input("password:") # 判斷賬號是否存在(自己寫入已存在用戶的賬號密碼) file = open("C:/Users/Lenovo/Desktop/user.txt","r") # 用于拼接文本內(nèi)容 file_data = "" while True: line = file.readline() if not line: break file_data += line line_content = line.strip() # 判斷是否存在賬號 if account == line_content: no_existence_flag = False true_password = file.readline() file_data += true_password true_password_content = true_password.strip() disable_flag_line = file.readline() disable_flag_num = int(disable_flag_line.strip()) # 判斷賬號是否被禁用 if disable_flag_num != 3: print("It is not disable!",disable_flag_num) disable_flag = False # 判斷密碼是否正確 if password == true_password_content: no_flag = False print("Welcome in this system,{account}!".format(account = account)) go = False disable_flag_line = disable_flag_line.replace(str(disable_flag_num),str(0)) file_data += disable_flag_line else: disable_flag_line = disable_flag_line.replace(str(disable_flag_num),str(disable_flag_num+1)) file_data += disable_flag_line else: file_data += file.readline() else: file_data += file.readline() file_data += file.readline() file.close() # 賬號不存在的報錯 if no_existence_flag: print("This account is not existence!") print("Do you want to try it again......") flag = input("Please input you think:") if flag == "N": go = False continue # 賬號被禁用的報錯 if disable_flag: print("You account is disable,please go home by youself!") print("Do you want to try it again......") flag = input("Please input you think:") if flag == "N": go = False continue # 賬號密碼錯誤的報錯 if no_flag: file = open("C:/Users/Lenovo/Desktop/user.txt","w") print(file_data) file.write(file_data) file.close() print("Your password is not right,please try it again!") print("Do you want to try it again......") flag = input("Please input you think:") if flag == "N": go = False # 重置輸入次數(shù) else: file = open("C:/Users/Lenovo/Desktop/user.txt","w") print(file_data) file.write(file_data) file.close()