首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将值永久添加到python字典中

如何将值永久添加到python字典中
EN

Stack Overflow用户
提问于 2018-11-07 19:43:25
回答 1查看 549关注 0票数 1

我有一个脚本,检查用户是否输入与正确密码相对应的有效用户名,并授予访问权限。它还允许他们创建用户名和密码,并将它们添加到字典中。但是,当我运行程序并创建一个新的用户名和密码时,它会将它们添加到字典中,而不是永久地添加到字典中,就像再次运行程序和打印字典时一样,我之前添加的值不再存在。

代码语言:javascript
复制
    # creates a users dictionary
users = {
    'Joe': 'juk725',
    'Mat': 'axr3',
    'Th3_j0k3r': 'bl4z3'
}
while username not in users and username != 'signup':
    username = input("enter username(type signup to create an account): ")

    if username == "signup" or username == "Signup":
        create_username = input("enter a new username: ")
        create_password = input("enter a new password: ")

    if create_password in users:
        create_password = input("password taken re-enter: ")
    if create_username==create_password:
        create_password=input("password cannot be the same as your username re-enter: ")
    # then adds the new username to the users dictionary
    if username == 'signup':
        users[create_username] = create_password

else:
    if username in users:
        password = input("enter password: ")
        if password in users:
           print("access granted")
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-07 19:45:39

Python只能访问操作系统分配给它的内存。当Python进程结束时,该内存不再可访问。如果您想要更多的永久存储,您可以写入磁盘。

有很多方法可以做到这一点,但是下面是Python中常用的几种字典:

  1. 泡菜
  2. json
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53196675

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档