Python:从用户输入引用Json文件

bqucvtff  于 2023-11-20  发布在  Python
关注(0)|答案(3)|浏览(127)

我目前正在使用JSON制作一个用户名/密码程序,但我有一个重复帐户的问题。我试图编写一种方法来防止用户创建JSON数据库已经包含的用户名,但它不太工作。

问题

  • 询问用户名,即使尝试的文件为空,也不询问密码
  • 有时说用户名已经存在,但无论如何都会创建重复的帐户。
    我想让程序做什么
  • 请输入新的用户名/密码
  • 如果用户名是唯一的,则将新帐户放入文件中
  • 如果用户名已被拥有,则不要添加新帐户并转到函数的开头。

这是我试过的代码,但是我提到的问题使它无效

def createUser():
    global accounts
    nUsername = input("Create Username »  ")
    for item in accounts:
        if item[0] == nUsername:
            return "Already Exsists!"
        else:
            nPassword = input("Create Password » ")
            entry = [nUsername, nPassword]
            accounts.append(entry)
            accounts = accounts[:500000]
            autoSave()

字符串
对于任何想知道的人来说,这就是autosave()函数:

def autoSave():
    with open("Accounts.json", "w") as outfile:
        json.dump(accounts, outfile)


下面是JSON文件的内部:

[["ExampleUsername", "BadPasswrdo14130"]]

9cbw7uwe

9cbw7uwe1#

有很多错误,所以我将使用注解来解释更改:

# you file containt utf8 chars, you need to specify encoding
# coding=utf-8

import os
import json

# I use a dict structure instead of a list for easier retrieval
# you can easily see if an account exist and get its password
# also global keyword is to avoid, so prefer declaring in the global context instead of pushing to the global context
accounts = {}

# if we have a file, deserialize content
if os.path.exists("/tmp/Accounts.json"):
    try:
        with open("/tmp/Accounts.json") as f:
            accounts = dict(json.loads(f.read()))
    except:
        pass

def createUser():
    # input is equivalent to eval(raw_input(... which is not the goal here
    nUsername = raw_input("Create Username »  ")

    # with a dict, no need to iterate through, simply use `in`
    if nUsername in accounts.keys():
        return createUser()

    nPassword = raw_input("Create Password » ")
    # this is how you assign the new account
    accounts[nUsername] = nPassword
    autoSave()

def autoSave():
    with open("/tmp/Accounts.json", "w") as outfile:
        # we convert here the dict to your list structure
        json.dump(list(accounts.iteritems()), outfile)

def existingUser():
    eUsername = raw_input("Your Username » ")
    ePassword = raw_input("Your Password » ")

    for item in accounts:
        if eUsername in accounts and accounts[eUsername] == ePassword:
            return 'Processing Sucessfully logged into your account!'
        else:
            return "Login failed"

createUser()

字符串

brccelvz

brccelvz2#

我会这样做:

# This function will help us to check if the user already exists
def alreadyExist(nUsername):
    global accounts
    for account in accounts:
        if account[0] == nUsername
            return True
    return False

def createUser():
    global accounts

    # We declarate nUsername first as None
    nUsername = None

    # While the username exists, we have to ask the user for the username
    while not nUsername or alreadyExist(nUsername):
        nUsername = input("Create Username »  ")

    # We are out of the bucle. The username doesn't exist, we can ask for the password
    nPassword = input("Create Password » ")
    entry = [nUsername, nPassword]
    accounts.append(entry)
    accounts = accounts[:500000]
    autoSave()

字符串

bvn4nwqk

bvn4nwqk3#

修正了我代码中的一个问题,但又出现了另一个问题。

def existingUser():
eUsername = input("Your Username » ")
ePassword = input("Your Password » ")

for item in accounts: #no need for braces
    if item[0] == eUsername and item[1] == ePassword:
        return 'Processing Sucessfully logged into your account!'
    else:
        return "Login failed"

字符串

相关问题