python用戶登錄模塊(不使用函數等方法)
阿新 • • 發佈:2018-04-13
wan 機會 sss www isp color 方便 sed ons
* 用戶登錄模塊
給定用戶信息表,需要滿足條件如下:
1. 輸入用戶名密碼判斷
2. 輸入錯誤次數3次時,詢問用戶是否需要繼續嘗試,Y繼續,N結束
3. 可支持多用戶登錄
1 # 方案一:輸入用戶名後立即判斷一次,共三次 2 li = [{‘username‘: ‘qqq‘, ‘password‘: ‘www‘}, 3 {‘username‘: ‘aaa‘, ‘password‘: ‘sss‘}, 4 {‘username‘: ‘zzz‘, ‘password‘: ‘xxx‘}] 5 6 # 將原列表轉換為方便比較的新字典 7 new_users_info = {}判斷用戶名是否正確8 for i in li: 9 new_users_info[i[‘username‘]] = i[‘password‘] 10 11 count_try_username, count_try_password = 0, 0 # 用戶名嘗試次數,密碼嘗試次數 12 flag_username, flag_password, flag_selection = 1, 1, 1 # 循環到用戶名輸入,循環到密碼輸入,循環到是否繼續 13 while flag_username: 14 count_try_username += 1 15 username_input = input(‘請輸入用戶名:‘) 16 if username_input in new_users_info.keys(): # 判斷用戶名是否存在 17 count_try_password = 0 # 用戶名嘗試次數歸零/或歸1 18 flag_password, flag_selection = 1, 1 # 所有循環重新開始 19 while flag_password: 20 password_input = input(‘請輸入密碼:‘) 21 if password_input == new_users_info[username_input]: #判斷密碼是否與用戶名匹配 22 print(‘登錄成功!跳轉到APP使用頁面‘) 23 flag_username = 0 24 break 25 else: 26 count_try_password += 1 27 print(‘密碼輸入錯誤...%s次‘ % count_try_password) 28 if count_try_password == 3: 29 while flag_selection: 30 user_selection = input(‘是否繼續?Y/N‘) 31 user_selection = user_selection.lower() 32 if user_selection == ‘y‘: 33 count_try_username, count_try_password = 0, 0 34 flag_password = 0 35 flag_selection = 0 36 elif user_selection == ‘n‘: 37 flag_username = 0 38 flag_password, flag_selection = 0, 0 39 print(‘ByeBye...‘) 40 else: 41 print(‘錯誤選擇‘) 42 else: 43 print(‘用戶名輸入錯誤...%s次‘ % count_try_username) 44 if count_try_username == 3: 45 flag_password, flag_selection = 1, 1 # 所有循環重新開始 46 while flag_selection: 47 user_selection = input(‘是否繼續?Y/N‘) 48 user_selection = user_selection.lower() 49 if user_selection == ‘y‘: 50 count_try_username = 0 51 flag_password = 0 52 flag_selection = 0 53 elif user_selection == ‘n‘: 54 flag_username = 0 55 flag_password, flag_selection = 0, 0 56 print(‘ByeBye...‘) 57 else: 58 print(‘錯誤選擇‘)
以上代碼會判斷用戶名錯誤三次的情況.
以下代碼不判斷用戶名錯誤,只判斷匹配.
1 # 用戶登錄(三次機會重試),可以支持多用戶登錄,三次後可選擇Y/N決定是否繼續嘗試 2 # 方案二:不判斷用戶名錯誤次數,只判斷密碼是否錯誤 3 li = [{‘username‘: ‘li_alex‘, ‘password‘: ‘SB‘}, 4 {‘username‘: ‘wu_sir‘, ‘password‘: ‘sb‘}, 5 {‘username‘: ‘xu_jin‘, ‘password‘: ‘student‘}] 6 7 new_user_info = {} 8 for user_info in li: 9 new_user_info[user_info[‘username‘]] = user_info[‘password‘] 10 print(new_user_info) 11 12 while 1: 13 count = 0 14 username_entry = input(‘Please Input an Username: ‘) 15 if username_entry in new_user_info.keys(): 16 password_entry = input(‘Please Input the Password: ‘) 17 while count < 2 and password_entry != new_user_info[username_entry]: 18 count += 1 19 print(‘Wrong Password Input, %d time(s) left...‘ % (3 - count)) 20 password_entry = input(‘Please Input the Password: ‘) 21 else: 22 if password_entry == new_user_info[username_entry]: 23 print(‘Congratulations! Login Succeed!‘) 24 break 25 elif count > 1: 26 user_selection = input(‘Invalid time Exceeds Limited, Do You Wanna Try Again? Y/N ‘) 27 lower_entry = user_selection.lower() 28 if lower_entry == ‘y‘: 29 continue 30 elif lower_entry == ‘n‘: 31 print(‘You Give up Login attempt. Good luck...(手動微笑)‘) 32 break 33 else: 34 print(‘Wrong Selection...Please Try Again...‘) 35 continue 36 else: 37 print("error")判斷用戶名和密碼匹配
均不使用函數等方法,只用while/for/if-else循環
python用戶登錄模塊(不使用函數等方法)