Python文件操作模擬用戶(hù)登陸代碼實(shí)例
題目要求
1、輸入用戶(hù)名和密碼后回車(chē)
2、密碼輸入錯(cuò)誤,給出提示,并選擇是否重新輸入
3、密碼輸入錯(cuò)誤三次后,用戶(hù)被鎖定,無(wú)法繼續(xù)登陸
構(gòu)思
1、用戶(hù)輸入賬號(hào)和密碼后,需要判斷賬號(hào)是否存在
2、判斷賬號(hào)是否被禁用(錯(cuò)誤次數(shù)大于三次)
3、判斷賬號(hào)密碼是否正確
4、不同的錯(cuò)誤給出不同的提示
5、每輸入錯(cuò)一次,文檔中的錯(cuò)誤次數(shù)需要更新
6、如果三次以?xún)?nèi)用戶(hù)登陸成功,密碼原來(lái)的錯(cuò)誤次數(shù)被重置
題目完成步驟
1、文檔的編寫(xiě)
考慮到數(shù)據(jù)的存儲(chǔ)問(wèn)題,決定將賬號(hào)、密碼、錯(cuò)誤次數(shù)進(jìn)行分行存儲(chǔ),三行為一組用戶(hù)信息
2、代碼編寫(xiě)
go = Truewhile go: # 用來(lái)判斷賬號(hào)是否存在 no_existence_flag = True # 用來(lái)判斷是否輸入正確 no_flag = True # 用來(lái)判斷是否已經(jīng)被封 disable_flag = True # 用來(lái)判斷次數(shù)是否已經(jīng)超過(guò)限制 account = input('account:') password = input('password:') # 判斷賬號(hào)是否存在(自己寫(xiě)入已存在用戶(hù)的賬號(hào)密碼) 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() # 判斷是否存在賬號(hào) 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()) # 判斷賬號(hào)是否被禁用 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_lineelse: 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() # 賬號(hào)不存在的報(bào)錯(cuò) 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 # 賬號(hào)被禁用的報(bào)錯(cuò) 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 # 賬號(hào)密碼錯(cuò)誤的報(bào)錯(cuò) 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()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. PHP設(shè)計(jì)模式中工廠模式深入詳解2. JSP數(shù)據(jù)交互實(shí)現(xiàn)過(guò)程解析3. .NET中l(wèi)ambda表達(dá)式合并問(wèn)題及解決方法4. 解決AJAX返回狀態(tài)200沒(méi)有調(diào)用success的問(wèn)題5. ThinkPHP5實(shí)現(xiàn)JWT Token認(rèn)證的過(guò)程(親測(cè)可用)6. asp(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說(shuō)明7. 利用promise及參數(shù)解構(gòu)封裝ajax請(qǐng)求的方法8. CSS hack用法案例詳解9. Ajax實(shí)現(xiàn)表格中信息不刷新頁(yè)面進(jìn)行更新數(shù)據(jù)10. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向
