How to implement Dictionary with Python3?
Last Updated :
21 Mar, 2024
This program uses python’s container called dictionary (in dictionary a key is associated with some information). This program will take a word as input and returns the meaning of that word.
Python3 should be installed in your system. If it not installed, install it from this link. Always try to install the latest version.
I made a text file in which word and its meaning is stored in python’s dictionary format
Example :
data = {"geek" : "engage in or discuss computer-related tasks
obsessively or with great attention to technical detail."}
Here if we call “geek” from data then this will return its meaning “engage in or discuss computer-related tasks obsessively or with great attention to technical detail.”.
This python program allow you to fetch the data of this text file and give the meaning.
import json
from difflib import get_close_matches
data = json.load( open ( "data.txt" ))
def meaning(w):
w = w.lower()
if w in data:
return data[w]
elif len (get_close_matches(w, data.keys())) > 0 :
yn = input ("Did you mean % s instead? Enter Y if yes, or N if no:
" % get_close_matches(w, data.keys())[ 0 ])
if yn = = "Y" :
return data[get_close_matches(w, data.keys())[ 0 ]]
elif yn = = "N" :
return "The word doesn't exist in our data."
else :
return "We didn't understand your entry."
else :
return "The word doesn't exist in our data."
word = input ( "Enter word: " )
output = meaning(word)
if type (output) = = list :
for item in output:
print (item)
else :
print (output)
|
How to run?
- Download data file and save it in the same folder where your python code file is saved.
- Make sure that both the file(data file and the code file) are in same folder.
- Open command prompt in that folder to do so press shift then right click in mouse.
- Run the python code using cmd(command prompt).
- Input the word whose meaning is to be searched.
- Output will be your result.
Video Demonstration
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...