Challenge

Project 1 - English Dictionary App & Library Book Management System

Q1:-This project uses the skills learnt from week 1 to week 6. The aim of this project is to create an English Dictionary app that returns definitions of English words. When the application is started, it displays a menu as follows:

Main Menu

Add a new word
Find the meaning
Update a word
Exit

Enter Choice:

If item 1 is selected, it should prompt for a word and then accept its meaning. This pair of word and its meaning should be stored in a file called ‘words.txt’. When item 2 is selected, it should prompt for a word, search that word in the file ‘words.txt’ and return the meaning if it is found. If the meaning is not found, it should display an appropriate message. Similarly, when choice 3 is entered, it should accept a word and update its meaning. In all the above three cases, the prompt should return to the main menu. The words and their meanings should be stored as a dictionary in words.txt, using the concept of serialization. If choice 4 is selected, a graceful exit should be performed.


If you want to be recognized as a professional programmer, you have to practice more and develop applications in Python which you can include in your online portfolio. To achieve this, one such comprehensive project (with two small sub-projects inside it) is added which have to be completed by every student.

1A: English Dictionary App

This project uses the skills learnt from week 1 to week 6. The aim of this project is to create an English Dictionary app that returns definitions of English words.

1B: Library Book Management System

In this project, a Library Book Management System will be implemented with the front end GUI developed using Tkinter and the backend database implemented in SQLite. It is a single window tkinter GUI application which will implement CRUD( create, read, update, delete) operations on a ‘books’ database, in SQLite. The window will contain text widgets to accept / display book title, author, year of publication, ISBN (optional) and a large text area/ list box to display the records which match the search criteria.

Answer:-

import tkinter as tk
from tkinter import messagebox
import sqlite3
import pickle

# Helper function for English Dictionary App
def add_word():
word = input("Enter the word: ").lower()
meaning = input("Enter the meaning: ")

# Load existing dictionary or create a new one
try:
with open('words.txt', 'rb') as file:
words_dict = pickle.load(file)
except FileNotFoundError:
words_dict = {}

# Add new word and meaning to the dictionary
words_dict[word] = meaning

# Save the updated dictionary to file
with open('words.txt', 'wb') as file:
pickle.dump(words_dict, file)

print(f"\n'{word}' added successfully!")

def find_meaning():
word = input("Enter the word to find its meaning: ").lower()

# Load dictionary from file
try:
with open('words.txt', 'rb') as file:
words_dict = pickle.load(file)
except FileNotFoundError:
print("\nDictionary is empty. Please add words first.")
return

# Find and display the meaning
if word in words_dict:
print(f"\nThe meaning of '{word}' is: {words_dict[word]}")
else:
print(f"\n'{word}' not found in the dictionary.")

def update_word():
word = input("Enter the word to update: ").lower()

# Load dictionary from file
try:
with open('words.txt', 'rb') as file:
words_dict = pickle.load(file)
except FileNotFoundError:
print("\nDictionary is empty. Please add words first.")
return

# Check if the word exists
if word in words_dict:
new_meaning = input(f"Enter the new meaning for '{word}': ")
words_dict[word] = new_meaning

# Save the updated dictionary to file
with open('words.txt', 'wb') as file:
pickle.dump(words_dict, file)

print(f"\n'{word}' updated successfully!")
else:
print(f"\n'{word}' not found in the dictionary.")

# Helper functions for Library Book Management System
def create_table():
# Connect to SQLite database
connection = sqlite3.connect('library.db')
cursor = connection.cursor()

# Create 'books' table if it doesn't exist
cursor.execute('''
CREATE TABLE IF NOT EXISTS books (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
author TEXT NOT NULL,
year INTEGER NOT NULL,
isbn TEXT
)
''')

# Commit changes and close the connection
connection.commit()
connection.close()

def add_book(title, author, year, isbn):
# Connect to SQLite database
connection = sqlite3.connect('library.db')
cursor = connection.cursor()

# Insert a new book into the 'books' table
cursor.execute('INSERT INTO books (title, author, year, isbn) VALUES (?, ?, ?, ?)', (title, author, year, isbn))

# Commit changes and close the connection
connection.commit()
connection.close()

def display_books():
# Connect to SQLite database
connection = sqlite3.connect('library.db')
cursor = connection.cursor()

# Retrieve all books from the 'books' table
cursor.execute('SELECT * FROM books')
books = cursor.fetchall()

# Close the connection
connection.close()

return books

def update_book(book_id, title, author, year, isbn):
# Connect to SQLite database
connection = sqlite3.connect('library.db')
cursor = connection.cursor()

# Update details of the selected book
cursor.execute('''
UPDATE books
SET title=?, author=?, year=?, isbn=?
WHERE id=?
''', (title, author, year, isbn, book_id))

# Commit changes and close the connection
connection.commit()
connection.close()

def delete_book(book_id):
# Connect to SQLite database
connection = sqlite3.connect('library.db')
cursor = connection.cursor()

# Delete the selected book from the 'books' table
cursor.execute('DELETE FROM books WHERE id=?', (book_id,))

# Commit changes and close the connection
connection.commit()
connection.close()

# Main GUI application
class LibraryApp:
def __init__(self, root):
self.root = root
self.root.title("Library Book Management System")

# Create 'books' table if it doesn't exist
create_table()

# GUI elements
self.label_title = tk.Label(root, text="Title:")
self.label_author = tk.Label(root, text="Author:")
self.label_year = tk.Label(root, text="Year:")
self.label_isbn = tk.Label(root, text="ISBN:")

self.entry_title = tk.Entry(root)
self.entry_author = tk.Entry(root)
self.entry_year = tk.Entry(root)
self.entry_isbn = tk.Entry(root)

self.listbox_books = tk.Listbox(root, width=40, height=10)

self.button_add = tk.Button(root, text="Add Book", command=self.add_book)
self.button_display = tk.Button(root, text="Display Books", command=self.display_books)
self.button_update = tk.Button(root, text="Update Book", command=self.update_selected)
self.button_delete = tk.Button(root, text="Delete Book", command=self.delete_selected)
self.button_exit = tk.Button(root, text="Exit", command=root.destroy)

# Grid placement
self.label_title.grid(row=0, column=0)
self.label_author.grid(row=1, column=0)
self.label_year.grid(row=2, column=0)
self.label_isbn.grid(row=3, column=0)

self.entry_title.grid(row=0, column=1)
self.entry_author.grid(row=1, column=1)
self.entry_year.grid(row=2, column=1)
self.entry_isbn.grid(row=3, column=1)

self.listbox_books.grid(row=0, column=2, rowspan=4)

self.button_add.grid(row=4, column=0)
self.button_display.grid(row=4, column=1)
self.button_update.grid(row=4, column=2)
self.button_delete.grid(row=4, column=3)
self.button_exit.grid(row=5, column=1, columnspan=2)

def add_book(self):
title = self.entry_title.get()
author = self.entry_author.get()
year = self.entry_year.get()
isbn = self.entry_isbn.get()

add_book(title, author, year, isbn)
messagebox.showinfo("Success", "Book added successfully!")

def display_books(self):
self.listbox_books.delete(0, tk.END)
books = display_books()
for book in books:
self.listbox_books.insert(tk.END, f"{book[1]} by {book[2]} ({book[3]}) - ISBN: {book[4]}")

def update_selected(self):
selected_index = self.listbox_books.curselection()
if not selected_index:
messagebox.showwarning("Error", "Please select a book to update.")
return

book_id = selected_index[0] + 1 # Assuming book IDs start from 1
title = self.entry_title.get()
author = self.entry_author.get()
year = self.entry_year.get()
isbn = self.entry_isbn.get()

update_book(book_id, title, author, year, isbn)
messagebox.showinfo("Success", "Book updated successfully!")

def delete_selected(self):
selected_index = self.listbox_books.curselection()
if not selected_index:
messagebox.showwarning("Error", "Please select a book to delete.")
return

book_id = selected_index[0] + 1 # Assuming book IDs start from 1
delete_book(book_id)
messagebox.showinfo("Success", "Book deleted successfully!")

if __name__ == "__main__":
while True:
print("\nMain Menu")
print("1. English Dictionary App")
print("2. Library Book Management System")
print("3. Exit")

choice = input("Enter Choice: ")

if choice == '1':
while True:
print("\nEnglish Dictionary App Menu")
print("1. Add a new word")
print("2. Find the meaning")
print("3. Update a word")
print("4. Back to Main Menu")

dict_choice = input("Enter Choice: ")

if dict_choice == '1':
add_word()
elif dict_choice == '2':
find_meaning()
elif dict_choice == '3':
update_word()
elif dict_choice == '4':
break
else:
print("Invalid choice. Please enter a valid option.")

elif choice == '2':
root = tk.Tk()
app = LibraryApp(root)
root.mainloop()

elif choice == '3':
print("Exiting the application. Goodbye!")
break

else:
print("Invalid choice. Please enter a valid option.")

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.