write the coding for a dictionary app
- User
write the coding for a dictionary app
Assistant
Dictionary App Coding
Below is a simple implementation of a dictionary app using Python. This app will allow users to add, delete, and search for words in the dictionary.
Dictionary App Code
import json class DictionaryApp: def __init__(self, filename): self.filename = filename self.load_dictionary() def load_dictionary(self): try: with open(self.filename, 'r') as file: self.dictionary = json.load(file) except FileNotFoundError: self.dictionary = {} def save_dictionary(self): with open(self.filename, 'w') as file: json.dump(self.dictionary, file) def add_word(self, word, meaning): """Add a word to the dictionary""" if word in self.dictionary: print(f"Word '{word}' already exists in the dictionary.") else: self.dictionary[word] = meaning self.save_dictionary() print(f"Word '{word}' added to the dictionary.") def delete_word(self, word): """Delete a word from the dictionary""" if word in self.dictionary: del self.dictionary[word] self.save_dictionary() print(f"Word '{word}' deleted from the dictionary.") else: print(f"Word '{word}' not found in the dictionary.") def search_word(self, word): """Search for a word in the dictionary""" if word in self.dictionary: print(f"Meaning of '{word}': {self.dictionary[word]}") else: print(f"Word '{word}' not found in the dictionary.") def display_dictionary(self): """Display all words in the dictionary""" if not self.dictionary: print("Dictionary is empty.") else: for word, meaning in self.dictionary.items(): print(f"{word}: {meaning}") def main(): filename = 'dictionary.json' app = DictionaryApp(filename) while True: print("\nDictionary App Menu:") print("1. Add word") print("2. Delete word") print("3. Search word") print("4. Display dictionary") print("5. Exit") choice = input("Enter your choice: ") if choice == '1': word = input("Enter word: ") meaning = input("Enter meaning: ") app.add_word(word, meaning) elif choice == '2': word = input("Enter word to delete: ") app.delete_word(word) elif choice == '3': word = input("Enter word to search: ") app.search_word(word) elif choice == '4': app.display_dictionary() elif choice == '5': break else: print("Invalid choice. Please try again.") if __name__ == "__main__": main()How it Works
- The
DictionaryAppclass is initialized with a filename to store the dictionary data. - The
load_dictionarymethod loads the dictionary data from the file. If the file does not exist, it creates an empty dictionary. - The
save_dictionarymethod saves the dictionary data to the file. - The
add_word,delete_word, andsearch_wordmethods allow users to add, delete, and search for words in the dictionary. - The
display_dictionarymethod displays all words in the dictionary. - The
mainfunction creates an instance of theDictionaryAppclass and provides a menu-driven interface for users to interact with the dictionary.
Example Usage
- Run the script.
- Choose an option from the menu:
- Add a word: Enter
1, then enter the word and its meaning. - Delete a word: Enter
2, then enter the word to delete. - Search for a word: Enter
3, then enter the word to search. - Display dictionary: Enter
4. - Exit: Enter
5.
- Add a word: Enter
- The
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.