|
| 1 | +# **Dictionary Application** |
| 2 | + |
| 3 | +The "Dictionary Application" is a simple Python program that allows users to manage a dictionary of words, their meanings, and example sentences. This project is designed to help you practice your Python programming skills and gain experience in working with databases. It offers the following features: |
| 4 | + |
| 5 | +### Key Features: |
| 6 | + |
| 7 | +1. **Add Word:** Users can add new words to the dictionary, along with their meanings and example sentences. |
| 8 | +2. **List Words:** The application displays a list of all the words in the dictionary, including their meanings and example sentences. |
| 9 | +3. **Search Word:** Users can search for a specific word in the dictionary to find its meaning and example sentence. |
| 10 | +4. **Delete Word:** The application allows users to delete words from the dictionary. |
| 11 | + |
| 12 | +### Project Structure: |
| 13 | + |
| 14 | +- The program uses an SQLite database to store word data, with a table named "words" to manage entries. |
| 15 | +- It includes a `DictionaryApp` class that encapsulates the functionality of the application, making it organized and easy to maintain. |
| 16 | +- The user interacts with the application through a simple command-line interface. |
| 17 | + |
| 18 | +### How to Use: |
| 19 | + |
| 20 | +1. Run the Python program. |
| 21 | +2. Select from the available options to add words, list words, search for words, delete words, or exit the application. |
| 22 | + |
| 23 | +### Why This Project? |
| 24 | + |
| 25 | +This project serves as a practical exercise for beginner and intermediate Python programmers. It covers fundamental concepts such as working with databases, user input, and class-based application structure. By completing this project, you will improve your Python skills and be better equipped for more complex programming tasks. |
| 26 | + |
| 27 | + |
| 28 | +### Suggested Enhancements: |
| 29 | + |
| 30 | +To further expand and enhance this project, you can consider adding features like: |
| 31 | + |
| 32 | +- Update/Edit Word: Allow users to modify the meaning or example sentence of an existing word. |
| 33 | +- User Authentication: Implement user accounts and authentication for managing personal dictionaries. |
| 34 | +- Better User Interface: Develop a graphical user interface (GUI) for a more user-friendly experience. |
| 35 | + |
| 36 | +Feel free to explore and customize this project to suit your learning goals and interests. Happy coding! |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | +```python |
| 41 | +import sqlite3 |
| 42 | + |
| 43 | +class DictionaryApp: |
| 44 | + def __init__(self, db_name): |
| 45 | + self.conn = sqlite3.connect(db_name) |
| 46 | + self.cursor = self.conn.cursor() |
| 47 | + self.create_table() |
| 48 | + |
| 49 | + def create_table(self): |
| 50 | + self.cursor.execute(''' |
| 51 | + CREATE TABLE IF NOT EXISTS words ( |
| 52 | + id INTEGER PRIMARY KEY, |
| 53 | + word TEXT, |
| 54 | + meaning TEXT, |
| 55 | + sentence TEXT |
| 56 | + ) |
| 57 | + ''') |
| 58 | + self.conn.commit() |
| 59 | + |
| 60 | + def add_word(self, word, meaning, sentence): |
| 61 | + self.cursor.execute("INSERT INTO words (word, meaning, sentence) VALUES (?, ?, ?)", (word, meaning, sentence)) |
| 62 | + self.conn.commit() |
| 63 | + |
| 64 | + def list_words(self): |
| 65 | + self.cursor.execute("SELECT * FROM words") |
| 66 | + words = self.cursor.fetchall() |
| 67 | + return words |
| 68 | + |
| 69 | + def search_word(self, word): |
| 70 | + self.cursor.execute("SELECT * FROM words WHERE word=?", (word,)) |
| 71 | + word = self.cursor.fetchone() |
| 72 | + return word |
| 73 | + |
| 74 | + def delete_word(self, word): |
| 75 | + self.cursor.execute("DELETE FROM words WHERE word=?", (word,)) |
| 76 | + self.conn.commit() |
| 77 | + |
| 78 | + def close(self): |
| 79 | + self.conn.close() |
| 80 | + |
| 81 | +def main(): |
| 82 | + dictionary = DictionaryApp("words.db") |
| 83 | + |
| 84 | + while True: |
| 85 | + print("\nDictionary Application") |
| 86 | + print("1. Add Word") |
| 87 | + print("2. List Words") |
| 88 | + print("3. Search Word") |
| 89 | + print("4. Delete Word") |
| 90 | + print("5. Exit") |
| 91 | + |
| 92 | + choice = input("Select an operation: ") |
| 93 | + |
| 94 | + if choice == "1": |
| 95 | + word = input("Word: ") |
| 96 | + meaning = input("Meaning: ") |
| 97 | + sentence = input("Example Sentence: ") |
| 98 | + dictionary.add_word(word, meaning, sentence) |
| 99 | + print("Word added.") |
| 100 | + |
| 101 | + elif choice == "2": |
| 102 | + words = dictionary.list_words() |
| 103 | + if words: |
| 104 | + print("\nWord List:") |
| 105 | + for word in words: |
| 106 | + print(f"Word: {word[1]}, Meaning: {word[2]}, Example Sentence: {word[3]}") |
| 107 | + else: |
| 108 | + print("No words found.") |
| 109 | + |
| 110 | + elif choice == "3": |
| 111 | + word = input("Enter Word to Search: ") |
| 112 | + result = dictionary.search_word(word) |
| 113 | + if result: |
| 114 | + print(f"Word: {result[1]}, Meaning: {result[2]}, Example Sentence: {result[3]}") |
| 115 | + else: |
| 116 | + print("Word not found.") |
| 117 | + |
| 118 | + elif choice == "4": |
| 119 | + word = input("Enter Word to Delete: ") |
| 120 | + dictionary.delete_word(word) |
| 121 | + print("Word deleted.") |
| 122 | + |
| 123 | + elif choice == "5": |
| 124 | + dictionary.close() |
| 125 | + break |
| 126 | + |
| 127 | +if __name__ == "__main__": |
| 128 | + main() |
| 129 | +``` |
| 130 | + |
| 131 | +This program creates a class called `DictionaryApp` that allows users to add words, list words, search for words, and delete words. It uses an SQLite database named "words.db" to store word data. Before running the program, make sure to create the "words.db" SQLite database file in the same directory or change the database name accordingly. |
0 commit comments