Q7.

Create a menu drive Python program with a dictionary for words and their meanings. Write functions to add a new entry (word: meaning), search for a particular word and retrieve meaning, given meaning find words with the same meaning, remove an entry, display all words sorted alphabetically.

In [2]:
word_dict = {}


def create_dict():
    global word_dict
    word_dict = {}
    ch = "y"
    while (ch == "y") or (ch == "Y"):
        print("\nEnter word:", end="")
        word = input()
        print("\nEnter meaning:", end="")
        meaning = input()
        word_dict[word] = meaning
        print("\nDo you want to continue adding words(y or n):", end="")
        ch = input()


def add_word():
    global word_dict
    print("\nEnter word:", end="")
    word = input()
    print("\nEnter meaning:", end="")
    meaning = input()
    word_dict[word] = meaning


def find_meaning(w):
    return word_dict[w]


def find_word_same_meaning(mng):
    words = []
    for w, m in word_dict.items():
        if mng == m:
            words.append(w)
    return words


def display_sorted():
    for w, m in word_dict.items():
        print(f"{w} ==> {m}")
    print("Sorted list of words : ")
    print(sorted(word_dict.keys()))


def main():
    ch = "y"
    while ch == "Y" or ch == "y":
        print("1: Create new dictionary")
        print("2: Add new word")
        print("3: Find meaning")
        print("4: Find word with same meaning")
        print("5: Display sorted list of words")
        print("6: Quit")
        print("Enter Choice: ", end="")
        option = int(input())
        if option == 1:
            create_dict()
        elif option == 2:
            add_word()
        elif option == 3:
            print("Enter word:", end="")
            word = input()
            print("Meaning:%s" % (find_meaning(word)))
        elif option == 4:
            print("Enter meaning:", end="")
            meaning = input()
            print("Words with same meaning:", end="")
            print(find_word_same_meaning(meaning))
        elif option == 5:
            display_sorted()
        elif option == 6:
            exit()
            

        print("\nDo you want to continue(y or n)?", end="")
        ch = input()


if __name__ == "__main__":
    main()
1: Create new dictionary
2: Add new word
3: Find meaning
4: Find word with same meaning
5: Display sorted list of words
6: Quit
Enter Choice: 1

Enter word:a

Enter meaning:apple

Do you want to continue adding words(y or n):y

Enter word:b

Enter meaning:bat

Do you want to continue adding words(y or n):y

Enter word:c

Enter meaning:cat

Do you want to continue adding words(y or n):y

Enter word:d

Enter meaning:dog

Do you want to continue adding words(y or n):n

Do you want to continue(y or n)?y
1: Create new dictionary
2: Add new word
3: Find meaning
4: Find word with same meaning
5: Display sorted list of words
6: Quit
Enter Choice: 2

Enter word:e

Enter meaning:egg

Do you want to continue(y or n)?y
1: Create new dictionary
2: Add new word
3: Find meaning
4: Find word with same meaning
5: Display sorted list of words
6: Quit
Enter Choice: 2

Enter word:kitten

Enter meaning:cat

Do you want to continue(y or n)?y
1: Create new dictionary
2: Add new word
3: Find meaning
4: Find word with same meaning
5: Display sorted list of words
6: Quit
Enter Choice: 3
Enter word:b
Meaning:bat

Do you want to continue(y or n)?y
1: Create new dictionary
2: Add new word
3: Find meaning
4: Find word with same meaning
5: Display sorted list of words
6: Quit
Enter Choice: 4
Enter meaning:cat
Words with same meaning:['c', 'kitten']

Do you want to continue(y or n)?y
1: Create new dictionary
2: Add new word
3: Find meaning
4: Find word with same meaning
5: Display sorted list of words
6: Quit
Enter Choice: 5
a ==> apple
b ==> bat
c ==> cat
d ==> dog
e ==> egg
kitten ==> cat
Sorted list of words : 
['a', 'b', 'c', 'd', 'e', 'kitten']

Do you want to continue(y or n)?n