无法将JSON文件加载为字典

时间:2020-03-25 09:50:03

标签: python json

我在Visual Studio代码中的System中创建了一个Login,用户可以在其中创建一个新帐户。此信息应以Dict的形式存储在JSON文件中,其中用户名为密钥,密码为值。到目前为止,几乎所有东西都可以正常工作,除了带有dict的JSON文件不会在Python中加载,我也不知道为什么。

import json

status = ""

def displayMenu():
    loadFile()
    print(users.keys())
    saveFile()
    status = input("Are you registered user? y/n? Press q to quit")
    if status == "y":
        oldUser()
    elif status == "n":
        newUser()


def newUser():
    createLogin = input("Create login name: ")

    if createLogin in users:
        print("\nLogin name already exist!\n")
    else:
        createPassw = input("Create password: ")
        users[createLogin] = createPassw
        print("\nUser created\n")

def oldUser():
    login = input("Enter login name: ")
    passw = input("Enter password: ")

    if login in users and users[login] == passw:
        print("\nLogin successful!\n")
    else:
        print("\nUser doesn't exist or wrong password!\n")

def saveFile():
    with open('personal.json', 'a') as json_file:
        json.dump(users, json_file)

def loadFile():
    with open('personal.json', 'r') as json_file:
        users = json.load(json_file)

while status != "q":
    displayMenu()

0 个答案:

没有答案