3

I have a JSON file that just has this:

 {
  "jeff": {

   }
 }

I would like to check if i enter the name jeff in my input it will just print 123 I have tried it this way in python:

import json
from pprint import pprint

with open('users.json') as f:
    data = json.load(f)

print("Hi, would you like to  sign up or login?")
print("Type Login for login and Signup to signup")

option= input()

if option == "Login":
    unameEntry = input("Please Enter Your Username")
    if unameEntry == data[unameEntry] :
        print("123")

but it does not find it i have looked around on google and have strougled to find an answer

0

2 Answers 2

1

What is wrong

When you access the value in your dictionary with:

data[unameEntry]

the dictionary returns you the value at data[unameEntry]. Suppose that, as you have set in your example, the user types in "jeff": following your example the dictionary will return the corresponding value, in your example an empty dictionary.

Therefore your if is actually doing:

"jeff" == {}

that is clearly False.

How to fix it

Most probably you want to check if the user is a key in the dictionary. You can do so by using the in operator:

unameEntry in data

Edited code

Since here we do not have access to your users.json file, let's include the dictionary in the code. Also, to detect if errors are found within the input handling, I'll through in a couple of asserts and expand the conditions tree:

data = {
    "jeff": {}
}

print("Hi, would you like to  sign up or login?")
print("Type Login for login and Signup to signup")

option= input()

assert option == "Login"

if option == "Login":
    unameEntry = input("Please Enter Your Username")
    assert unameEntry == "jeff"
    if unameEntry in data:
        print("Welcome %s!" % unameEntry)
    else:
        print("Username %s does not exist" % unameEntry)
Sign up to request clarification or add additional context in comments.

7 Comments

i changed it to this
if unameEntry in data : print("123")
I have edited the code to help you debugging further. How does it do now?
Hi, would you like to sign up or login? Type Login for login and Signup to signup Login Please Enter Your Usernamejeff Username jeff does not exist ] >>>
I have added another assert, does it throw the exception now?
|
0

Made a few tweaks to what you have, and added some comments for detail. Here's what the Python would look like:

# Python3
import json


# Load json file one-liner
file_json = json.load(open("test.json", "r"))

# Since options can only be signup/login, santize them
option = input("Type Login for login and Signup to signup: ").strip().lower()
if option == "login":
    # Get name entry and strip it of whitespace
    name_entry = input("Please Enter Your Username: ").strip()
    # Use .get() on file_json dict() object, with a default option
    # of None. However, with this you actually have to add data to 
    # your JSON and not just a empty field
    if file_json.get(name_entry, None):
        print("123")
    else:
        print("Name: {}, not found".format(name_entry))
else:
    print("unrecognized option: {}".format(option))

And here's what the test JSON should look like:

{
    "jeff": {
        "foo": "bar"
    }
}

Notice how I've added a default value to "jeff". Else, doing a lookup will always return nothing when you've got { } as "jeff"'s value

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.