好的,对于我正在做的Python任务,我必须建立一个密码系统,允许用户输入用户名和密码并进行更改,但挑战的最后一部分是让用户永久记住系统。我考虑过使用文本文件进行存储,但这样做很麻烦。这是我的代码:
import os
import time
accounts = {}
started = ""
while started != "q":
started = input("Are you a registered user y/n: (if you want to quit press any key)")
if started == "n":
createLogin = input("Create your desired login name: ")
if createLogin in accounts:
print ("Login name already exist!")
else:
createPassw = input("Create your desired password password: ")
accounts[createLogin] = createPassw
print("User created!")
elif started == "y":
login = input("Enter your login name: ")
if login in accounts:
passw = input("Enter your password")
else:
print("user doesnt exist.")
if login in accounts and accounts[login] == passw:
print("Login successful!")
changepassword = input("Would you like to change your password?")
if changepassword in ["Y", "y"]:
newpassw = input("What do you want your new password to be?")
accounts[login] = newpassw
print("password succesfully changed!")
else:
print("Password was not changed.")
else:
print("program closing!")
break
那么有没有办法真正做到这一点,如果是这样,因为我尝试了pickle和一个文本文件,但它不起作用。我非常感谢你的帮助。
答案 0 :(得分:1)
您想要的是基于文件的数据库。这在python中很容易。使用Pickle模块。
使用该模块,您可以序列化(转换为文本或位流),然后将其保存在您想要的任何文件上。在下一个程序运行时,您加载该文件并反序列化。