简体   繁体   English

如何在一个泡菜词典中存储多个项目?

[英]How to store multiple items in one pickle dictionary?

I am currently attempting to make a login/signup program on my computer that will allow for multiple sets of usernames and passwords. 我目前正在尝试在计算机上制作一个登录/注册程序,该程序将允许使用多组用户名和密码。 Right now anytime I sign up, it overwrites the previous login. 现在,无论何时我注册,它都会覆盖以前的登录信息。 I am using Python 3.4. 我正在使用Python 3.4。

Is there a way I can prevent this? 有什么办法可以防止这种情况吗?

My code is available below: 我的代码如下:

import os
import pickle
import sys
import time

user_name = 'default'
pass_word = '12345'

login = {'username' : user_name,
         'password' : pass_word}

def cls():
    os.system('cls')
def space():
    print(' ')

def load():
    with open('logins', 'rb') as f:
        login = pickle.load(f)
def save():
    with open('logins', 'wb') as f:
        pickle.dump(login, f)

def MainMenu():
    print('Select an Option.')
    while True:
        print('1) Login')
        print('2) Signup')
        user_input = input('Option #: ')
        if user_input == '1':
            cls()
            login_user()
        elif user_input == '2':
            cls()
            signup_user()
        else:
            cls()
            continue

def signup_user():
    user_chosen_name = input('Username: ')
    login['username'] = user_chosen_name
    user_chosen_password = input('Password: ')
    login['password'] = user_chosen_password
    space()
    cls()
    print('Setup complete. Please login.')
    os.system('pause')
    save()
    cls()
    login_user()

def login_user():
    load()
    while True:
        print('Please Login.')
        space()
        user_input_name = input('Username: ')
        user_input_password = input('Password: ')
        if user_input_name == login['username'] and user_input_password == login['password']:
            space()
            print('Login Successful.')
        else:
            space()
            print('Login Failed. Please Try Again.')
            while True:
                print('1) Try Again.')
                print('2) Main Menu.')
                user_cont = input('Continue?: ')
                if user_cont == '1':
                    cls()
                    break
                elif user_cont == '2':
                    cls()
                    MainMenu() 
                    break

if __name__ == '__main__':
    if os.path.isfile('logins') == False:
        save()
    else: 
        pass
    MainMenu()

Here are two proposals for the login/password data model. 这是有关登录名/密码数据模型的两个建议。

  1. Use a dictionary, this is probably the simplest way ; 使用字典,这可能是最简单的方法; I suggest using this. 我建议使用这个。

     # init with default passwords = {user_name: pass_word} # password look-up if login in passwords: print passwords[login] else: print 'User', login, 'is not registered' # password set or update password[login] = new_password 
  2. List of couples or list of dictionaries. 夫妻名单或字典名单。 This may be closer to your current solution, but I would not recommend it. 这可能更接近您当前的解决方案,但我不建议这样做。 I only show what the initialization would be. 我只显示初始化是什么。

     # list of couples logins = [(user_name, pass_word)] # list of dictionaries logins = [{'username' : user_name, 'password' : pass_word}] 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM