1. 程式人生 > >【每日一練】裝飾器

【每日一練】裝飾器

count 其他 用戶登陸 glob 每日一練 div class 密碼 try

寫一個用戶登錄認證函數,要求:

1.要求用戶輸入賬號密碼和txt中的用戶名數據庫對比;

2.要求用戶有三次嘗試機會;

3.要求用戶登陸後,執行其他功能無需再驗證;

txt數據:

{‘auth_name‘:‘user0‘, ‘auth_passwd‘:‘123‘}
{‘auth_name‘:‘user1‘, ‘auth_passwd‘:‘123‘}
{‘auth_name‘:‘user2‘, ‘auth_passwd‘:‘123‘}
{‘auth_name‘:‘user3‘, ‘auth_passwd‘:‘123‘}
 1 auth_state = {auth_name: None, auth_log
: False} 2 count = 3 3 4 5 def auth(fun): 6 def wap(*args, **kwargs): 7 if auth_state[auth_name] and auth_state[auth_log]: 8 res = fun(*args, **kwargs) 9 return res 10 global count 11 while 1: 12 if count > 0: 13 user = input(
enter your auth name:\n).strip() 14 passwd = input(enter your auth passwd:\n).strip() 15 with open(test, r) as f: 16 for line in f: 17 line = eval(line) 18 if line[auth_name] == user and line[
auth_passwd] == passwd: 19 auth_state[auth_name] = user 20 auth_state[auth_log] = True 21 res = fun(*args, **kwargs) 22 return res 23 else: 24 print(user_name or passwd is wrong) 25 count -= 1 26 continue 27 else: 28 print(three times faile, try later.) 29 break 30 31 return wap 32 33 34 @auth 35 def log(): 36 print(welcome back) 37 38 39 @auth 40 def home(): 41 print(welcome to home) 42 43 44 log() 45 home()

 

【每日一練】裝飾器