Project 1 English Dictionary App
import json
d = {}
lst = list()
def add(x,y):
file = open("words.txt",'r')
for line in file:
for key,val in d.items():
if (val == ''):
del d[key]
key, val = line.split('\n')
d[(key)] = val
file.close()
d[x] = y
with open("words.txt",'a') as f:
for key,val in d.items():
f.write('%s:%s\n' % (key, val))
def search(x):
d1={}
filename = "words.txt"
with open(filename,"r") as f:
for line in f:
(key,val) = line.strip().split(":")
d1[key] = val
for k,v in d1.items():
if k==x:
return d1[x]
else:
print("Try spelling the word correctly in mixed case like 'Apple'")
return False
def change(x,y):
d2={}
with open("words.txt", "r") as f:
for line in f:
(key,val) = line.strip().split(":")
d2[key] = val
for k,v in d2.items():
if k==x:
d2[x] = y
else:
print("No such word exists")
f.close()
with open("words.txt",'w') as f:
for key,val in d2.items():
f.write('%s:%s\n' % (key, val))
return d2[x]
while True:
print("\nMain Menu")
print("1.Add new word")
print("2.Find the meaning")
print("3.Update a word")
print("4.Exit")
choice = int(input("Enter your choice"))
if choice == 1:
print("Add new word")
word = input("Enter word")
mean = input("Enter the meaning of the word")
add(word,mean)
print("Word:",word,"\tDefinition:",mean)
elif choice == 2:
print("Find the meaning")
w = input("Enter a word")
print("The meaning of",w,":",search(w))
elif choice == 3:
print("Update a word")
w = input("Enter a word")
m = input("Enter the new meaning")
print("The updated meaning of",w,":",change(w,m))
elif choice == 4:
break
## this program can be further extended to exclude factors such as case of the values entered, misspelt values and so on.