2017-09-08 104 views
0

我想創建一個Python腳本,用戶可以在其中創建帳戶並登錄。我需要保存他們的帳戶詳細信息(名稱,DoB等),以便他們可以關閉程序和日誌在下次再次。Python 3.6中的用戶名和密碼

#username:Password 
account = {"Admin":["Pog"],"Hughes":["Hughes"], "E": ["Echo"], "C": ["Charlie"], "B": ["Bravo"], "D": ["Delta"], "A": ["Alpha"]} 

#Check Credentials 
name=input("Username: ")     #Ask for name 
if name in account:       #Check name exists 
    print (account[name][0])    #Error test - print expected password 
    if passw==account[name][0]:    #Check for password match 
     print("welcome,", name)    #Welcome message 
else: 
    print ("Failed Authentication")  #Wrong Password 
else: 
    print("Username Not Known")    #Unknown Username 

上面的代碼工作,我可以驗證用戶,但如何添加新用戶/密碼,這樣他們將被添加到該計劃?

我一直在嘗試從txt文件寫入/讀取,但沒有成功。這是正確的方法嗎?

任何幫助將不勝感激!

感謝

(我試圖尋找,但無法找到我要找的。How to create a python dictionary that will store the username and password for multiple accounts接近)

+0

請修復您的代碼縮進。正確的做法是數據庫,你可以啓動sqlite或MySql,但不能使用文本文件。 – jps

+1

固定壓痕 – MrPogson

回答

0

是的,你需要使用一個外部文件作爲常量存儲要不然使用某種數據庫來存儲這些數據。否則,當你重新啓動腳本,它會這就是當前在內存中(最後一個腳本運行過程中存儲)放下一切

0

使用我解決我的問題泡椒:

...

account = pickle.load(open("accounts.pkl","rb")) #Load Accounts file 
#Check Credentials 
name=input("Username: ")     #Ask for name 
if name in account:       #Check name exists 
    print (account[name][0])    #Error test - print expected password 
    passw=input("Password: ")    #Ask for Password 
    if passw==account[name][0]:    #Check for password match 
     print("welcome,", name)    #Welcome message 
     print("your Favourite Artist is: ",account[name][2]) 
     menu(name,account) 
    else: 
     print ("Failed Authentication")  #Wrong Password 
     users() 
else: 
    print("Username Not Known")    #Unknown Username 
    new_user(account) 

在哪裏帳戶詳細信息存儲如下:

{'Name': ['Password', 'Detail_1', 'Detail_2', 'Detail_3'],'Name2': ['Password', 'Detail_12', 'Detail_22', 'Detail_32']}