Project 1 - English Dictionary App & Library Book Management System
English Dictionary App
In [1]:
import pickle
def load_dictionary():
"""
Loads the dictionary from the 'words.txt' file using pickle.
If the file does not exist, returns an empty dictionary.
"""
try:
with open('words.txt', 'rb') as file:
return pickle.load(file)
except FileNotFoundError:
return {}
def save_dictionary(dictionary):
"""
Saves the dictionary to the 'words.txt' file using pickle.
"""
with open('words.txt', 'wb') as file:
pickle.dump(dictionary, file)
def add_word():
"""
Adds a new word and its meaning to the dictionary.
Prompts the user to enter the word and meaning.
Writes the updated dictionary to the 'words.txt' file using save_dictionary().
"""
dictionary = load_dictionary()
word = input('Enter the word: ')
meaning = input('Enter the meaning: ')
dictionary[word] = meaning
save_dictionary(dictionary)
print(f'{word} added to dictionary.')
def find_meaning():
"""
Searches for a word in the dictionary and returns its meaning if found.
Prompts the user to enter the word to search for.
"""
dictionary = load_dictionary()
word = input('Enter the word: ')
if word in dictionary:
print(f'{word}: {dictionary[word]}')
else:
print(f'{word} not found in dictionary.')
def update_word():
"""
Updates the meaning of an existing word in the dictionary.
Prompts the user to enter the word to update and its new meaning.
Writes the updated dictionary to the 'words.txt' file using save_dictionary().
"""
dictionary = load_dictionary()
word = input('Enter the word to update: ')
if word in dictionary:
meaning = input('Enter the new meaning: ')
dictionary[word] = meaning
save_dictionary(dictionary)
print(f'{word} updated in dictionary.')
else:
print(f'{word} not found in dictionary.')
def main():
"""
Displays the main menu and calls the appropriate function based on user input.
"""
while True:
print('Main Menu\n1. Add a new word\n2. Find the meaning\n3. Update a word\n4. Exit\n')
choice = input('Enter Choice: ')
if choice == '1':
add_word()
elif choice == '2':
find_meaning()
elif choice == '3':
update_word()
elif choice == '4':
break
else:
print('Invalid choice. Please try again.\n')
if __name__ == '__main__':
main()
Library Book Management System
In [1]:
import tkinter as tk
from tkinter import messagebox
import sqlite3
# Create the database connection
conn = sqlite3.connect('books.db')
c = conn.cursor()
# Create the table
c.execute('''CREATE TABLE IF NOT EXISTS books
(title TEXT, author TEXT, year INTEGER, isbn INTEGER)''')
conn.commit()
# Create the main window
root = tk.Tk()
root.title('Book List')
# Create the listbox
book_list = tk.Listbox(root, height=10)
book_list.grid(row=0, column=0, columnspan=4, padx=10, pady=10, sticky="nsew")
# Create the entry fields
title_label = tk.Label(root, text='Title:')
title_label.grid(row=1, column=0, padx=5, pady=5)
title_entry = tk.Entry(root)
title_entry.grid(row=1, column=1, padx=5, pady=5)
author_label = tk.Label(root, text='Author:')
author_label.grid(row=2, column=0, padx=5, pady=5)
author_entry = tk.Entry(root)
author_entry.grid(row=2, column=1, padx=5, pady=5)
year_label = tk.Label(root, text='Year:')
year_label.grid(row=3, column=0, padx=5, pady=5)
year_entry = tk.Entry(root)
year_entry.grid(row=3, column=1, padx=5, pady=5)
isbn_label = tk.Label(root, text='ISBN:')
isbn_label.grid(row=4, column=0, padx=5, pady=5)
isbn_entry = tk.Entry(root)
isbn_entry.grid(row=4, column=1, padx=5, pady=5)
# Function to add a book to the database and listbox
def add_book():
title = title_entry.get()
author = author_entry.get()
year = year_entry.get()
isbn = isbn_entry.get()
if title:
c.execute("INSERT INTO books VALUES (?, ?, ?, ?)", (title, author, year, isbn))
conn.commit()
book = f"{title} by {author}, {year}, ISBN: {isbn}"
book_list.insert(tk.END, book)
title_entry.delete(0, tk.END)
author_entry.delete(0, tk.END)
year_entry.delete(0, tk.END)
isbn_entry.delete(0, tk.END)
else:
messagebox.showwarning('Warning', 'Please enter a book title.')
# Function to remove a book from the database and listbox
def remove_book():
selection = book_list.curselection()
if selection:
book = book_list.get(selection)
title, author, year, isbn = book.split(',')
c.execute("DELETE FROM books WHERE title=? AND author=? AND year=? AND isbn=?", (title.strip(), author.strip(), year.strip(), isbn.strip()))
conn.commit()
book_list.delete(selection)
else:
messagebox.showwarning('Warning', 'Please select a book to remove.')
# Function to show all books in the database and listbox
def show_all_books():
books = []
for row in c.execute("SELECT * FROM books"):
book = f"{row[0]} by {row[1]}, {row[2]}, ISBN: {row[3]}"
books.append(book)
if books:
messagebox.showinfo('All Books', '\n'.join(books))
else:
messagebox.showinfo('All Books', 'No books in list.')
# Function to search for a book in the database and listbox
def search_book():
title = title_entry.get()
author = author_entry.get()
year = year_entry.get()
isbn = isbn_entry.get()
books = []
query = "SELECT * FROM books WHERE "
parameters = []
if title:
query += "title=? AND "
parameters.append(title)
if author:
query += "author=? AND "
parameters.append(author)
if year:
query += "year=? AND "
parameters.append(year)
if isbn:
query += "isbn=? AND "
parameters.append(isbn)
query = query.rstrip("AND ")
for row in c.execute(query, parameters):
book = f"{row[1]} by {row[2]}, {row[3]}, ISBN: {row[4]}"
books.append(book)
if books:
messagebox.showinfo('Search Results', '\n'.join(books))
else:
messagebox.showinfo('Search Results', 'No books found.')
# Create the buttons
add_button = tk.Button(root, text='Add', command=add_book)
add_button.grid(row=5, column=0, padx=10, pady=5)
remove_button = tk.Button(root, text='Remove', command=remove_book)
remove_button.grid(row=5, column=1, padx=10, pady=5)
show_all_button = tk.Button(root, text='Show All', command=show_all_books)
show_all_button.grid(row=6, column=0, padx=10, pady=5)
search_book_button = tk.Button(root, text='Search Book', command=search_book)
search_book_button.grid(row=6, column=1, padx=10, pady=5)
# Start the main loop
root.mainloop()