Project 1 - English Dictionary App & Library Book Management System
import pickle
import os
def read_dictionary(): # function to read the file
try :
with open('words.txt','rb') as dictionary:
temp_dict= pickle.load(dictionary)
print('reading from dictionary')
return temp_dict
except Exception as e:
print(f'error: [{e}]')
def writing_dictionary(record):
try:
with open('words.txt','wb') as dictionary: # function to write into the file words.txt
pickle.dump(record,dictionary)
print('record entered successfully')
except Exception as e:
print(f'error: {e}')
def Addnewword(): # function to add new word
print('find the meaning')
word=input('enter word: ')
print('enter the meaning: ')
meaning=input('meaning: ')
dictionary=read_dictionary()
dictionary[word]=meaning
writing_dictionary(dictionary)
main_function()
def findword(): # function to find the meaning of entered word
print('enter the word and find the meaning ')
word=input('word: ')
dictionary=read_dictionary()
if word in dictionary:
print(f'MEANING TO THE WORD { word}: {dictionary[word]}')
else :
print('the word does not exit in dictionary')
main_function()
def updateword(): # function to update the meaning of entered word
print('update the meaning')
print('enter the word')
word=input('word: ')
dictionary=read_dictionary()
if word in dictionary:
print(f'the word and meaning are [{word}: {dictionary[word]}')
new_meaning=input('enter new meaning: ')
dictionary[word]=new_meaning
writing_dictionary(dictionary)
print(f'the meaning updated to the word[{word}: {dictionary[word]}]')
main_function()
# the main function
def main_function():
if not os.path.exists(os.path.join("words.txt")):
try:
with open('words.txt', 'wb') as dictionary:
pickle.dump({}, dictionary)
print("SUCCESS: writing to dictionary words.txt")
except Exception as e:
print(f"ERROR: {e}")
print("Welcome to the English dictionary app ")
print("---------------------- ")
print('Main menu')
print('1.Add a new word')
print('2.Find the meaning')
print('3.Update a word')
print('4.Exit')
print(" --------------------------")
option= input("enter the option: ")
if option == '1':
Addnewword()
elif option == '2':
findword()
elif option == '3':
updateword()
elif option == '4':
exit()
else :
print('wrong choice: enter a vallid option')
main_function()
# the main funciton call
main_function()