1. 程式人生 > >Python 模擬三次用戶登錄,登錄失敗鎖定賬號並寫入文件

Python 模擬三次用戶登錄,登錄失敗鎖定賬號並寫入文件

python3 列表 文件讀寫

登錄要求:

1、用戶輸入登錄賬號 、密碼,登錄成功顯示歡迎用戶登錄界面

2、 用戶可以嘗試三次登錄,三次登錄失敗後,將鎖定用戶,並輸出到新的文件當中。

count = 0
while count < 3 :
    file1 = open("accounts.txt", "r")
    data1 = file1.read().strip().split()
    username = input("Please input your username>>>> ")
    password = input("Please input your password>>>> ")
    if username == data1[0] and password == data1[1]:
        print("welcom to %s login" %username)
        break;
    elif username == data1[2] and password == data1[3]:
        print("welcom to %s login" % username)
        break;
    elif username == data1[4] and password == data1[5]:
        print("welcom to %s login" % username)
        break;
    else:
        print("user  %s login is failure please try again" % username)
    count = count + 1
else:
    file2 = open("lock_userlist.txt", "w")
    file2.write(username)
    print("you have try 3 times your %s is locked\n" %username)

accounts.txt

技術分享圖片


測試:登錄成功

技術分享圖片


測試 :登錄失敗

技術分享圖片

技術分享圖片

Python 模擬三次用戶登錄,登錄失敗鎖定賬號並寫入文件