我对 Python 非常陌生,所以毫无疑问有几个非常基本的错误,尽管我已经阅读了几个 Python 教程并且一切似乎都很好。
我正在尝试将两个文本文件导入到我的列表中。
username.txt
看起来像这样
用户1 用户 2
用户
3
password.txt
看起来像这样
传
1传2
传3
当我运行所有内容时,调试器不会给我任何错误,但是当我尝试user/password
组合时,它说它不正确。
你能看出有什么不对吗,或者知道有什么可以帮助的吗?
这就是我正在运行的:
from Tkinter import *
username = ["nathan"]
password = ["1"]
def login():
index = 0
usrnm = usernameEntry.get()
while index < len(username):
if username[index] == usrnm:
psswrd = passwordEntry.get()
if password[index] == psswrd:
print("Success")
break
else:
print("Password doesn't exist!")
break
else:
index = index + 1
else:
print("Username doesn't exist!")
def save():
fWUsername = open("username.txt", "w")
fWUsername.write("\n".join(str(x) for x in username))
fWUsername.close
fWPassword = open("password.txt", "w")
fWPassword.write("\n".join(str(x) for x in password))
fWPassword.close
def load():
fRUsername = open("username.txt", "r")
username = fRUsername.readlines()
fRUsername.close()
fRPassword = open("password.txt", "r")
password = fRPassword.readlines()
fRPassword.close()
loginForm = Tk()
usernameLabel = Label(loginForm, text = "username:")
usernameLabel.pack()
usernameEntry = Entry(loginForm)
usernameEntry.pack()
passwordLabel = Label(loginForm, text = "password:")
passwordLabel.pack()
passwordEntry = Entry(loginForm, show = "*")
passwordEntry.pack()
submitButton = Button(loginForm, text = "submit", command = login)
submitButton.pack()
loadButton = Button(loginForm, text = "load data lists", command = load)
loadButton.pack()
saveButton = Button(loginForm, text = "save data lists", command = save)
saveButton.pack()
mainloop()