我正在嘗試構建一個工作登錄功能,該功能會將我帶到我的菜單中。 下面是我的代碼有:如何讓我的登錄功能正常工作
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
什麼是'userlist'? 'dict','list list of list'等等? – ZWiki
userlist是一個文本文件,看起來像這樣: Hunter password1 Charles password – syntax
你如何閱讀它? – ZWiki