1. 程式人生 > >三次登入練習

三次登入練習

版權宣告

作者: 張志強(xeon)
出處: https://www.cnblogs.com/xeoon
郵箱: [email protected]
本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段宣告,且在文章頁面明顯位置給出原文連線。通過本部落格學習的內容,造成任何後果均與本人無關!!!

作業:使用者登入

  1. 三次重試機會
  2. 每次輸錯誤時顯示剩餘錯誤次數

方法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:

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:

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:

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