2015-11-25 42 views
1

我正在嘗試構建一個工作登錄功能,該功能會將我帶到我的菜單中。 下面是我的代碼有:如何讓我的登錄功能正常工作

def login(userlist): 
    verified = False 
    name = input('username: ') 
    password = input('password: ') 
    while not verified: 
     name = input('username: ') 
     password = input('password: ') 
     if [name, password] in userlist: 
      verified = True 
      print('You logged in the system!') 
     else: 
      print('invalid username\password.') 
    return verified 

即使我在正確的用戶名和密碼輸入到程序中,它口口聲聲說無效... 我缺少什麼?此外,userlist是一個文本文件,用戶名和密碼由行分隔。我能夠讀取該文件,因爲我的負載功能:

def load(): 
    user_file = open('userdata.txt', 'r') 
    userlist = [] 
    line = user_file.readline() 
    while line != '': 
     line = line.split() 
     userlist.append(line) 
     line = user_file.readline() 
    user_file.close() 
    return userlist 

    My text file ends up looking like this: 

用戶名#password口令#list

山姆#password口令#[1,2,...]

(名單的數字實際上並沒有溢出到下一行) 這是我的註冊功能,我認爲這是相關的。

def register(userlist): 
# ASK FOR USERNAME 
name = input('Choose your username: ') 
while search(userlist, name): 
    print('The username is not available. Choose somethng else. ') 
    name = input('Choose your username: ') 
# ASKING FOR PASSWORD 
password = input('Choose your password: ') 
p = input('Retype your password: ') 
while p != password: 
    password = input('Choose your password: ') 
    p = input('Retype your password: ') 

# SURVEY INDEX 

a = list(range(26)) 
user = [name, p, a] 
userlist.append(user) 
print('Your registration is successful, your user namw ia: ', name) 
user_file = open('userdata.txt', 'a') 
user_file.write(str(name) + ' ' + str(p) + ' ' + str(a)+ '\n') 
user_file.close() 

return userlist 
+1

什麼是'userlist'? 'dict','list list of list'等等? – ZWiki

+0

userlist是一個文本文件,看起來像這樣: Hunter password1 Charles password – syntax

+0

你如何閱讀它? – ZWiki

回答

0

如果用戶列表是文件IO對象:

for i in userlist.readlines(): 
    if [name, password] in i.split() # assumes no whitespace in password is allowed 
     verified = True 
     print('You logged in the system!') 

如果userlist是一個字典:

if (name,password) in userlist.items() 
+0

userlist是一個文本文件,其中包含每個用戶的用戶名和密碼在一條分開的線上 – syntax

0

你必須與你的負載功能的問題。當您檢查[name, password]是否在用戶列表中時,您並未將用戶名和密碼一起存儲,它們位於用戶列表中的單獨列表中。

所以我改變了加載函數來做你想做的事情。 雖然假設你的文本文件看起來像這樣,但我更改了加載函數(缺少換行符也很好)。

編輯:好吧,我現在對我的答案進行了更改,現在我知道您的文本文件的樣子。

def login(userlist): 
    verified = False 
    print(userlist) 
    while not verified: 
     name = input('username: ') 
     password = input('password: ') 
     if [name, password] in userlist: 
      verified = True 
      print('You logged in the system!') 
     else: 
      print('invalid username\password.') 
    return verified 

def load(): 
    userlist = [] 
    with open('test.txt', 'r') as user_file: 
     for line in user_file: 
      line = line.strip() 
      if not line: 
       continue 
      userlist.append(line.split()[:2]) # Gets only name and password 
    return userlist 

if __name__ == '__main__': 
    user_list = load() 
    login(user_list) 

如果你想改變一個字典,而不是那麼你可以做到這一點

if [name, password] in userlist:成爲if userlist.get(name) == password:

userlist = []成爲userlist = {}

userlist.append(line.split()[:2])成爲

name, password = line.split()[:2] 
userlist[name] = password 

邊注: 我建議改變你的寫字線,以更乾淨。

user_file.write("{0} {1} {2}\n".format(name, p, a)) 
+0

非常有幫助,謝謝! – syntax