└── Linear search /Linear search: -------------------------------------------------------------------------------- 1 | def display_menu(): 2 | print("Dictionary Program") 3 | print("1. Look up a word") 4 | print("2. Add a new word") 5 | print("3. Delete a word") 6 | print("4. Display all words") 7 | print("5. Exit") 8 | 9 | def look_up_word(dictionary): 10 | word = input("Enter the word to look up: ").strip() 11 | if word in dictionary: 12 | print(f"The meaning of '{word}' is: {dictionary[word]}") 13 | else: 14 | print(f"'{word}' not found in the dictionary.") 15 | 16 | def add_word(dictionary): 17 | word = input("Enter the word to add: ").strip() 18 | if word in dictionary: 19 | print(f"'{word}' already exists in the dictionary with the meaning: {dictionary[word]}") 20 | else: 21 | meaning = input(f"Enter the meaning of '{word}': ").strip() 22 | dictionary[word] = meaning 23 | print(f"'{word}' has been added to the dictionary.") 24 | 25 | def delete_word(dictionary): 26 | word = input("Enter the word to delete: ").strip() 27 | if word in dictionary: 28 | del dictionary[word] 29 | print(f"'{word}' has been deleted from the dictionary.") 30 | else: 31 | print(f"'{word}' not found in the dictionary.") 32 | 33 | def display_all_words(dictionary): 34 | if dictionary: 35 | print("Dictionary contents:") 36 | for word, meaning in dictionary.items(): 37 | print(f"{word}: {meaning}") 38 | else: 39 | print("The dictionary is empty.") 40 | 41 | def main(): 42 | dictionary = {} 43 | while True: 44 | display_menu() 45 | choice = input("Enter your choice: ").strip() 46 | if choice == '1': 47 | look_up_word(dictionary) 48 | elif choice == '2': 49 | add_word(dictionary) 50 | elif choice == '3': 51 | delete_word(dictionary) 52 | elif choice == '4': 53 | display_all_words(dictionary) 54 | elif choice == '5': 55 | print("Exiting the program. Goodbye!") 56 | break 57 | else: 58 | print("Invalid choice. Please try again.") 59 | 60 | if __name__ == "__main__": 61 | main() 62 | --------------------------------------------------------------------------------