Challenge

Project 1 English Dictionary App

#1. Database-Software Interface

import pickle #serialization package

def saveDictionary(tmp_dict):
'''
This function recives a python dictionary,
and saves it to a serialized file words.txt
'''
try:
# 'wb' means open file with writing binary mode
with open('words.txt', 'wb') as dictionary:
pickle.dump(tmp_dict, dictionary)
# dictionary = open('words.txt', 'wb')
# pickle.dump(tmp_dict, dictionary)
# dictionary.close()
print("SUCCESS: writing to dictionary words.txt")
except Exception as e:
print(f"ERROR: {e}")


def getDictionary():
'''
This function returns a python dictionary,
after de-serializing the file words.txt
'''
try:
# 'rb' means open file with reading binary mode
with open('words.txt', 'rb') as dictionary:
tmp_dict = pickle.load(dictionary)
print("SUCCESS: reading from dictionary words.txt")
return tmp_dict
except Exception as e:
print(f"ERROR: {e}")


#2. User-Software Interface

def addNewWord():
'''
This function provides functionalities for n==1,
that is, it creates a new word in the dictionary
'''
print("__________")
print("NEW WORD ADDER:")
print("__________")
print("Enter a Word to add")
word = input(">>")
print(f"Enter a meaning of {word} to add")
meaning = input(">>")
dictionary = getDictionary()
dictionary[word] = meaning
saveDictionary(dictionary)

# after the work of this function is done, we need to go back to the menu
main_menu()


def findMeaning():
'''
This function provides functionalities for n==2,
that is, it finds a word in the dictionary
'''
print("__________")
print("WORD FINDER:")
print("__________")
print("Enter a Word to find")
word = input(">>")
dictionary = getDictionary()
# check if the word exists
# if it does, print its meaning
if word in dictionary:
print(f"Found Entry: [{word} : {dictionary[word]}]")
else:
# otherwise throw an error and go back to the submenu of findMeaning()
print("ERROR: The word doesn't exist!")
findMeaning()

# after the work of this function is done, we need to go back to the menu
main_menu()


def updateWord():
'''
This function provides functionalities for n==3,
that is, it updates a word in the dictionary
'''
print("__________")
print("WORD UPDATER:")
print("__________")
print("Enter an old Word to edit")
word = input(">>")
# check if the word exists
# if it does, update it
dictionary = getDictionary()
if word in dictionary:
print(f"Enter a New Meaning to edit [{word} : {dictionary[word]}] to:")
new_meaning = input(">>")
dictionary[word] = new_meaning
saveDictionary(dictionary)
print(f"SUCCESS: Updated to [{word} : {dictionary[word]}]")
else:
# otherwise throw an error and go back to the submenu of updateWord()
print("ERROR: The word doesn't exist! impossible to update!")
updateWord()

# after the work of this function is done, we need to go back to the menu
main_menu()


def main_menu():
'''
This function prints Main Menu:
__________
ENGLISH DICTIONARY APP
__________
1. Add a new word
2. Find the meaning
3. Update a word
4. Exit
__________
'''
print("__________")
print("ENGLISH DICTIONARY APP")
print("__________")
print("1. Add a new word")
print("2. Find the meaning")
print("3. Update a word")
print("4. Exit")
print("__________")
# n is a way to choose items from the menu
n = int(input(">>"))
if n==1:
# if n==1 call the function that asks for a new word to save
addNewWord()
elif n==2:
# if n==2 call the function that asks for a word to search
findMeaning()
elif n==3:
# if n==3 call the function that asks for which word to edit
updateWord()
elif n==4:
# if n==4 then exit the program
exit()
else:
# otherwise, show the error, and print the menu again
print("ERROR: Please Enter a digit according to Menu.")
main_menu()


# Script to Run

main_menu() # to initiate the program
#print(getDictionary())
#dictionary = {}
#saveDictionary(dictionary)
Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.