2016-04-28 61 views
0

我想做一個銀行,其中存儲用戶輸入的用戶名和密碼在一個文件(文本文件)。現在,當用戶登錄時,他輸入他的用戶名。現在如何比較是否輸入此用戶名當他/她創建賬戶(存儲在passw.txt中)時,用戶與存儲的密碼相同。
這裏是我的代碼,如何將用戶輸入的用戶名與存儲在文件中的密碼進行比較?

store_user=open("user.txt","a") 
store_passw=open("passw.txt","a") 
print "Type 1 to create a new acc or 2 to log in" 
a=input("Your choice: ") 
if a==1: 

    a=raw_input("Enter your username: ") 
    store_user.write(a+'\n') 
    store_user.close() 

    b=raw_input("Enter pass: ") 
    store_passw.write(b+'\n') 
    store_passw.close() 
elif a==2: 
    a=raw_input("Enter username: ") 
    ''' 

    Now how do I comapare the username entered by the user with my file user.txt 
    And also it should check the same userame has same associated password with passw.txt 

    '''  

我也想知道的是,這種方法更容易或應我存儲的用戶名和密碼字典,然後在文件。如果它追我這樣做,那會怎樣我從中得到用戶名和密碼? (就像用戶輸入他的用戶名,現在我的程序如何進入存儲用戶名和密碼的文件,並檢查用戶名(密鑰)是否與密碼(值)匹配。

回答

0

您需要一個結構。你可以使用行號來獲得pair - user-password,這會帶來更多的問題,然後你可以期待,請使用結構,保持數據的一致性,例如你可以把它們放入單個文件 - 讀取整個記錄 - 名稱,密碼 - 簡單的方法 - 像逗號分隔值 - python有csv模塊 - https://docs.python.org/2/library/csv.html或者你可以去更復雜的東西 - JSON,XML - 這裏是鏈接json:https://docs.python.org/2/library/json.html

0

我個人不會存儲在2個文件中,而是使用兩個文件之間的分隔符元素和其他分隔符值之間該元素

例「的Test1,PASS1 \ nTest2,PASS2」將是分隔符「\ n」和「」

爲您的代碼,你將不得不找到linecount在文件1然後跳到另一個文件中該行比較密碼:

psudocode

open username 
linefound = -1 
for line,lineid in file 
    if line == user 
     linefound = lineid 
close username 
if linefound >= 0 
    open password 
    for line, lineid in file 
     if lineid == linefound 
      //compare do rest 
相關問題