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

python3.7-PyCharm 解釋器

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

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

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

圖1-3 成功登錄指針

圖1-4 用戶名被鎖定code

四、下面爲源碼:orm

__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’,便於提取文件內容。htm

相關文章
相關標籤/搜索