Created
November 18, 2018 07:47
-
-
Save Dhrumilcse/cb7a8d62430bc0f4aa1e7c0b83415139 to your computer and use it in GitHub Desktop.
Dictionary Part 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Import library | |
import json | |
#Loading the json data as python dictionary | |
#Try typing "type(data)" in terminal after executing first two line of this snippet | |
data = json.load(open("dictionary.json")) | |
#Function for retriving definition | |
def retrive_definition(word): | |
#Removing the case-sensitivity from the program | |
#For example 'Rain' and 'rain' will give same output | |
#Converting all letters to lower because out data is in that format | |
word = word.lower() | |
#Check for non existing words | |
#1st elif: To make sure the program return the definition of words that start with a capital letter (e.g. Delhi, Texas) | |
#2nd elif: To make sure the program return the definition of acronyms (e.g. USA, NATO) | |
if word in data: | |
return data[word] | |
elif word.title() in data: | |
return data[word.title()] | |
elif word.upper() in data: | |
return data[word.upper()] | |
#Input from user | |
word_user = input("Enter a word: ") | |
#Retrive the definition using function and print the result | |
print(retrive_definition(word_user)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment