Project 1 - English Dictionary App
The aim of this project is to create an English Dictionary app that returns definitions of English words. When the application is started, it displays a menu as follows:
Main Menu
Add a new word
Find the meaning
Update a word
Exit
Enter Choice:
If item 1 is selected, it should prompt for a word and then accept its meaning. This pair of word and its meaning should be stored in a file called ‘words.txt’. When item 2 is selected, it should prompt for a word, search that word in the file ‘words.txt’ and return the meaning if it is found. If the meaning is not found, it should display an appropriate message. Similarly, when choice 3 is entered, it should accept a word and update its meaning. In all the above three cases, the prompt should return to the main menu. The words and their meanings should be stored as a dictionary in words.txt, using the concept of serialization. If choice 4 is selected, a graceful exit should be performed.
import json
my_dict={}
print('Mainmenu nn 1.Add a word n 2.Find the meaning n 3.Update a word n 4.Exit n')
while True:
try:
action=int(input('Please select from the given mainmenu(1,2,3,4):'))
except ValueError :
print('!!!!Please enter interger values only!!!')
print('#'*50)
else:
if action==4:
e=input('Are you sure you want to exit?(yes/no):')
if e=='Yes' or e=='yes':
print('Thank you have a nice day!!')
print('#'*50)
break
else:
continue
elif action==1:
word=input('Enter a new word:')
meaning=input('Enter meaning of the word you entered:')
my_dict[word]=meaning
with open('words.txt','w')as f:
json.dump(my_dict, f)
#f.write('n')
print('Word added sucessfully')
print('#'*50)
elif action==2:
find=input('Enter a word to find its meaning:')
with open ('words.txt','r')as f:
my_dict=json.load(f)
if find in my_dict:
print(f'Meaning of the word you entered is : {my_dict[find]}')
print('#'*50)
continue
else:
print('Word not found')
print('#'*50)
elif action==3:
word=input('Enter a word to be updated:')
with open ('words.txt','r')as f:
my_dict=json.load(f)
if word in my_dict:
meaning=input('Enter new a meaning:')
my_dict[word]=meaning
with open("words.txt", "w") as f:
json.dump(my_dict, f)
print(f'has been updated to {meaning}')
print('#'*50)
continue
else:
print('Word not found')
print('#'*50)
else:
print('!!!Please chose the correct option from the Mainmenu!!!')
print('#'*50)