做業:用戶登陸python
方法1:學習
count = 0 number = 3 while count < 3: username = input('請輸入用戶名:') password = input('請輸入密碼:') if username == 'xeon' and password == '123456': print('用戶:', username, '登陸成功.') count = 3 break else: number -= 1 print('用戶:', username, '登陸失敗,剩餘錯誤次數爲', number) count += 1
方法2:code
username, password = 'xeon', '123456' count = 0 while count <= 3: username1 = input('請輸入用戶名:') password1 = input('請輸入密碼:') if username == username1 and password == password1: print('用戶:', username, '登陸成功.') break else: count += 1 number = 3 -count print('用戶名或密碼錯誤! 剩餘失敗錯誤次數爲: %s.' %number)
方法3:blog
username, password = 'xeon', '123456' count = 3 while count > 0: username1 = input('請輸入用戶名:') password1 = input('請輸入密碼:') if username == username1 and password == password1: print('用戶:', username, '登陸成功.') break else: count -= 1 if count == 0: print("\n用戶名或密碼錯誤次數過多,禁止登錄!") break print("用戶名或密碼錯誤,請再次輸入,剩餘嘗試次數 %s 次\n" % count)
方法4:ip
list1 = ['xeon', '123456'] count = 0 while count < 3: username = input('請輸入用戶名:').strip() password = input('請輸入密碼:').strip() if username == list1[0] and password == list1[1]: print('登陸成功') break else: print('用戶名或密碼錯誤,剩餘嘗試次數爲%d'% (2 - count)) count += 1