以下代码没有错误或问题我想" connect"这个脚本到外部文件或数据库,以便那个" my_dict"在执行脚本之后,字典可以存储和使用它的内容。 我正在寻找有关如何实现这一目标的想法。我并不是要求修复代码,因为它具有功能性。只是提供上下文。
my_dict = {}
class password(object):
def __init__(self):
pass
def username_input(self):
self.user = str(input("Please enter the username you would like to use: "))
return self.user
#gets the input for a username
def password_input(self):
self.passw = str(input("Please enter the password you would like to use: "))
return self.passw
#gets the input for a password
def username_creator(self):
password.username_input(self)
return self.user
def password_creator(self):
password.password_input(self)
return self.passw
def swap_username(self, new_pass, old_pass):
my_dict[new_pass] = my_dict.pop(old_pass)
def swap_password(self, new_pas, old_pas):
my_dict[old_pas] = new_pas
def username_change(self):
self.d = input("please enter your username: ")
for key in my_dict:
if key == self.d:
self.x = input("enter a new username: ")
password().swap_username(self.x, self.d)
print("your username has been updated!")
break
else:
print("the username you entered does not exist in our records")
password().username_change()
def password_change(self):
self.e = input("please enter your username: ")
self.q = input("please enter your password: ")
for key, value in my_dict.items():
if key == self.e and value == self.q:
self.z = input("enter a new password: ")
password().swap_password(self.z, self.e)
break
else:
print ('the combination entered does not exist in our records')
password().password_change()
make_a_username = password()
my_dict.update({(make_a_username.username_creator()): (make_a_username.password_creator())})
答案 0 :(得分:0)
实现此目的的一种方法是将my_dict
存储在文件中。所以基本上,无论何时启动应用程序,您都要从文件中加载my_dict
,然后在关闭之前,再次将更新的my_dict
存储在文件中。由于它是字典,您可以将其存储在JSON中。
所以,你最终要做的是:
// to save
json_string = json.dumps(datastore)
// now write this string to a file
// to load, first read json_string from file. Then,
my_dict = json.loads(json_string)