python版本為python3.5
1.要求
1)輸入用戶名密碼
2)認證成功后顯示歡迎信息
3)輸錯三次后鎖定
2.需求分析
1)用戶信息存儲在文件中(login/config/user_login.txt)
2)用戶輸入用戶名和密碼
3)判斷用戶名是否存在,存在則繼續,不存在則提示繼續輸入
4)判斷輸入的用戶名是否已經被鎖定,如果已鎖定則退出程序,否則繼續
5)匹配文件中的用戶信息
6)如果匹配則打印出歡迎信息
7)如果輸入3次密碼錯誤,則鎖定該用戶名(login/config/name_lock.txt)
3.測試用戶
bigberg:123abc
lc:123456
smallberg:111111
root:12345
dinasor:12321
# -*- coding: UTF-8 -*-#Author:Bigberg#定義一個循環計數count = 0#定義一個字典存儲用戶名和密碼names={}#定義一個列表存儲用戶名name_exit = []#輸入用戶名username = input("username:")#判斷輸入的用戶的用戶是否在用戶名單中with open("../config/name_login.txt",'r') as f: while True: line = f.readline().strip() if not line: break else: name = line.split(':')[0] passwd = line.split(':')[1] names[name] = passwdfor key in names: name_exit.append(key)#判斷用戶名是否正確,不正確則繼續輸入while username not in name_exit: print("The account is not exit.Check it again.") username = input("username:")else: # 讀取鎖定文件中的內容 with open("../config/name_lock.txt", "r") as f: lock_name = f.read() # 判斷用戶名是否在鎖定文件中,如果在就退出程序 if username == lock_name: print("Sorry.Your account has been locked.") exit() else: # 3次輸入密碼的機會 while count < 3: passwd_input = input("password:") # 判斷用戶名和密碼是否有效 if passwd_input == names[username]: print("Welcome!", username) break else: print("Error,please try again.") count += 1 # 如果3次輸入密碼錯誤,則將用戶名添加到鎖定文件 if count == 3: with open("../config/name_lock.txt", "w") as f: f.write("%s" % username) print("You have tried 3 times,and your account will be locked")
總結
以上就是本文關于Python基礎練習之用戶登錄實現代碼分享的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站:Python入門之三角函數全解【收藏】、python好玩的項目—色情圖片識別代碼分享、Python實現一個簡單的驗證碼程序等,有什么問題可以隨時留言,小編會及時回復大家的。感謝朋友們對本站的支持!
新聞熱點
疑難解答