python 實現簡單的用戶名-密碼驗證-file的讀寫練習


python3.7-PyCharm 解釋器

1、提示用戶輸入用戶名:
關於python的file讀寫操作請參見教程python3的file方法
用戶輸入用戶名后台到用戶名列表"name_init"文件檢查用戶名是否存在,若存在,則檢查用戶是否被鎖定,進行步驟2;若不存在,提示用戶注冊,輸入密碼和確認密碼(密碼不一致,提示錯誤),一致后將用戶名寫入"name_init"文件,同時將用戶名密碼以“用戶名:密碼”方式寫入“name_password_init”文件,提示注冊成功,然后成功登陸,見圖1-1:

2、檢查用戶是否被鎖定:若鎖定,則提示已被鎖定聯系管理員,見圖1-2;若未被鎖定,則提示用戶輸入密碼,進行步驟3.

3、提示用戶輸入密碼:若密碼正確,則提示登陸成功,見圖1-3;若不正確,則允許輸入n次,第n-1次時提示只有最后一次機會,連續錯了n次后,賬戶被鎖定,用戶名被寫入"locked_namelist",見下圖,例子中n=3.

圖1-3 成功登陸

圖1-4 用戶名被鎖定

4、下面為源碼:

__author__ = 'Administrator'
username = input("username:")
#print(username)
name_list = open('name_init','r+')  #'name_init' file created before running
name_text = name_list.readlines()
#print(name_text)

username_i = username+'\n'
if username_i not in name_text :
    print("User doesn't exist,please register.")
    continue_confirm = input("Do you want to register?...Y/N:")
    if continue_confirm == "n" or continue_confirm == "N":
        print("You are leaving.")
    else:
        password1 = input("Input the password:")
        confirm_password = input("Confirm the password:")
        if  password1 == confirm_password :
            name_w = username + '\n'
            name_list.write(name_w)
            name_list.close()

            n_p = username + ':'+password1+'\n'
            name_l_f = open('name_password_init','a+')  #'name_password_init' file created before running
            name_l_f.write(n_p)
            name_l_f.close()

            print("Registration success\n","Welcome user---{name}---login...".format(name=username))
        else:
            print('Incorrect input.')

else:
    locked_f = open("locked_namelist",'r+')
    locked_list = locked_f.readlines()
    if username+'\n' in locked_list :
        locked_f.close()
        print('You are locked,please contact the administrator for unlock')
    else:
        count = 0
        ct_l = 3
        name_l_f = open('name_password_init','r+')  #'name_password_init' file created before running
        namelist_f = name_l_f.readlines()
        while count <ct_l:
            password = input("password:")
            user_info = username+':'+password+'\n'
            if  user_info in namelist_f :
                print("Welcome user---{name}---login...".format(name=username))
                break
            else :
                if count < ct_l-2:
                    print('Incorrect passed,please try again.')

                elif count == ct_l-2 :
                    print('Warning:Only one chance left.')

                count += 1
                if count == ct_l :
                    locked_f.write(username+'\n')
                    locked_f.close()
                    print('The username was locked,please contact the administrator.')

        name_l_f.close()

第23行的文件打開模式為'a+',主要為了寫入,指針定位到文件的末尾:
name_l_f = open('name_password_init','a+')
第40行的文件打開模式為'r+',需要先讀取文件,指針從文件開頭開始:
name_l_f = open('name_password_init','r+')
file.readlines()讀取文件的所有內容,讀取的結果為列表格式,結果中帶有換行符‘\n’,所以文件寫入時也需要帶上‘\n’,便於提取文件內容。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM