From 83588644313278964c938616e92e508f11b077d0 Mon Sep 17 00:00:00 2001 From: Madhava Kulkarni Date: Tue, 4 Dec 2018 10:12:11 +0530 Subject: [PATCH] Update interactive-dictionary.py to make it interactive Update interactive-dictionary.py to make it interactive. Added while loop and ask if user wants to continue. Also, replace data.json to dictionary.json, as data.json does not exist. --- interactive-dictionary.py | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/interactive-dictionary.py b/interactive-dictionary.py index 9148b0e..cc588b7 100644 --- a/interactive-dictionary.py +++ b/interactive-dictionary.py @@ -4,7 +4,7 @@ #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("data.json")) +data = json.load(open("dictionary.json")) #Function for retriving definition def retrive_definition(word): @@ -34,17 +34,24 @@ def retrive_definition(word): return ("The word doesn't exist, yet.") else: return ("We don't understand your entry. Apologies.") +flag = True +while(flag): + #Input from user + word_user = input("Enter a word: ") -#Input from user -word_user = input("Enter a word: ") + #Retrive the definition using function and print the result + output = retrive_definition(word_user) -#Retrive the definition using function and print the result -output = retrive_definition(word_user) + #If a word has more than one definition, print them recursively + if type(output) == list: + for item in output: + print("-",item) + #For words having single definition + else: + print("-",output) -#If a word has more than one definition, print them recursively -if type(output) == list: - for item in output: - print("-",item) -#For words having single definition -else: - print("-",output) + ask = input("\nDo you want to search another word? [y/n]: ") + if(ask.lower() == 'y' or ask.lower() == 'yes'): + flag = True + else: + flag = False