Reputation:
I am currently new to Python.
I am making a dictionary app to learn.
However, I am having trouble getting the right output in one of my functions.
I want the user to input a word and get the word (key) and the definition (value) back in return.
The JSON file I am pulling from can be found here: https://github.com/prestonjohnson17/Dictionary
import json
data = json.load(open("data.json"))
type(data)
def finding_def():
user_word = data[input(str())]
if data.keys() == user_word:
print(user_word)
else:
print ("not a real word")
finding_def()
Upvotes: 0
Views: 90
Reputation: 659
This is cleaver but loses some of the readability in the process.
import json
data = json.load(open("data.json"))
print(data.get(input(), "not a real word"))
Upvotes: 1
Reputation: 5200
Try this.
import json
data = json.load(open("data.json"))
def finding_def():
user_word = input("Enter word: ")
value = data.get(user_word, "not a real word")
print(value)
finding_def()
Upvotes: 0
Reputation: 512
You should check if the key is present in the dictionary, and then get the value for that key (Although as I saw the JSON file, the value itself is an array; You should handle printing all entries of the array).
def finding_def():
user_word = input()
if user_word in data:
print("Entries:")
for entry in data[user_word]:
print(entry)
else:
print("not a real word")
finding_def()
Upvotes: 1