Challenge

Project 1 - English Dictionary App & Library Book Management System

1. The aim of this project is to create an English Dictionary app that returns definitions of English words.

import json

def load_words():
try:
with open('words.txt', 'r') as file:
return json.load(file)
except FileNotFoundError:
return {}

def save_words(words):
with open('words.txt', 'w') as file:
json.dump(words, file)

def add_word():
word = input("Enter the word: ")
meaning = input("Enter its meaning: ")

words = load_words()
words[word] = meaning
save_words(words)
print("Word added successfully!")

def find_meaning():
word = input("Enter the word to find its meaning: ")
words = load_words()

if word in words:
print(f"Meaning: {words[word]}")
else:
print("Word not found in the dictionary.")

def update_word():
word = input("Enter the word to update its meaning: ")
words = load_words()

if word in words:
meaning = input("Enter the updated meaning: ")
words[word] = meaning
save_words(words)
print("Word updated successfully!")
else:
print("Word not found in the dictionary.")

def main():
while True:
print("\nMain Menu\n")
print("1. Add a new word")
print("2. Find the meaning")
print("3. Update a word")
print("4. Exit\n")

choice = input("Enter Choice: ")

if choice == '1':
add_word()
elif choice == '2':
find_meaning()
elif choice == '3':
update_word()
elif choice == '4':
print("Exiting...")
break
else:
print("Invalid choice. Please try again.")

if __name__ == "__main__":
main()

2. Question

#creating an gui based dictionary app using python and some librarys
#IMPORTTING
import sqlite3

def initial_database(): #DEFINNING THE FUNCTIONS
'''
call me, i'll initiate and give you a connection and a cursor to my database
'''
connection = sqlite3.connect("LBMS.db")
cursor = connection.cursor()
print("Database connection established successfully")
creating_table(cursor)
return(connection, cursor) #returnning the output to the function


def terminate_database(connection): #DEFINNING THE FUNCTIONS
'''
give me a database connection i'll terminate it
'''
connection.close()
print("Database connection de-established successfully")

def creating_table(cursor): #DEFINNING THE FUNCTIONS
'''
give me a cursor to database i'll create book table in it
'''
sql_command = """CREATE TABLE if not exists books (
title varchar(60) not null,
author varchar(30) not null,
year INTEGER not null,
ISBN INTEGER,
unique (title, author, year, ISBN)
);"""
cursor.execute(sql_command)


def reading_table(cursor): #DEFINNING THE FUNCTIONS
'''
give me a cursor to database, i'll read it all and send you
'''
sql_command = "SELECT * FROM books"
cursor.execute(sql_command)
data = cursor.fetchall()
return(data) #returnning the output to the function


def adding_to_table(cursor, title, author, year, isbn): #DEFINNING THE FUNCTIONS
'''
give me a cursor to database, and a book, i'll add it to the books table
'''
sql_command = f"INSERT INTO books VALUES ('{title}', '{author}', {year}, {isbn});"
try:
cursor.execute(sql_command)
return(0, "success")
except sqlite3.IntegrityError as e:
return(111, e) #returnning the output to the function
except sqlite3.OperationalError as e:
return(112, e) #returnning the output to the function


def removing_from_table(cursor, title, author, year, isbn): #DEFINNING THE FUNCTIONS
'''
give me a cursor to database, and a book, i'll remove it.
'''
try:
sql_command = f'''DELETE FROM books WHERE title="{title}";'''
cursor.execute(sql_command)
sql_command = f'''DELETE FROM books WHERE author="{author}";'''
cursor.execute(sql_command)
sql_command = f'''DELETE FROM books WHERE year="{year}";'''
cursor.execute(sql_command)
sql_command = f'''DELETE FROM books WHERE isbn="{isbn}";'''
cursor.execute(sql_command)
except:
return(-1) #returnning the output to the function


def searching_from_table(cursor, title, author, year, isbn): #DEFINNING THE FUNCTIONS
'''
give me a cursor to database, and a book, i'll search it.
'''
try:
cursor.execute("SELECT * FROM books WHERE title=?", (title,))
searched_titles = cursor.fetchall()
cursor.execute("SELECT * FROM books WHERE author=?", (author,))
searched_authors = cursor.fetchall()
cursor.execute("SELECT * FROM books WHERE year=?", (year,))
searched_years = cursor.fetchall()
cursor.execute("SELECT * FROM books WHERE ISBN=?", (isbn,))
searched_isbns = cursor.fetchall()
return(searched_titles + searched_authors + searched_years + searched_isbns)
except:
return(-1) #returnning the output to the function


def saving_changes(connection):
'''
give me a connection to database i'll save the changes
'''
connection.commit()

# database_gui_interface

def listing_all_books(listArea): #DEFINNING THE FUNCTIONS
connection, cursor = initial_database()
print("listing books")
books = reading_table(cursor)
listArea.delete('0','end')
for i in range(len(books)):
listArea.insert(i,f"{books[i][0]}, {books[i][1]}, {books[i][2]}, {books[i][3]}")
terminate_database(connection)


def searching_books(listArea, title, author, year, isbn): #DEFINNING THE FUNCTIONS
connection, cursor = initial_database()
print("search book")
title = title.get()
author = author.get()
year = year.get()
isbn = isbn.get()
res = searching_from_table(cursor, title, author, year, isbn)
if res==-1:
messagebox.showerror("Error!", "something went wrong!")
if res!=-1:
listArea.delete('0','end')
for i in range(len(res)):
listArea.insert(i,res[i])
terminate_database(connection)


def adding_books(title, author, year, isbn): #DEFINNING THE FUNCTIONS
connection, cursor = initial_database()
print("adding book")
title = title.get()
author = author.get()
year = year.get()
isbn = isbn.get()
code, error = adding_to_table(cursor, title, author, year, isbn)
if code==111:
messagebox.showerror("Error!", f"Book Already Exists!\n\nerror code: {code}\nerror:{error}")
if code==112:
messagebox.showerror("Error!", f"Please, fill in the input fields appropriately.\n\ntitle: string, author: string, year: integer, year: integer.\n\nerror code: {code}\nerror:{error}")
saving_changes(connection)
terminate_database(connection)

def issue_book(title, author, year, isbn): #DEFINNING THE FUNCTIONS
connection, cursor = initial_database()
print("issue book")
messagebox.showwarning("Undefined", "this function is not yet defined!")
terminate_database(connection)

def deleting_book(title, author, year, isbn): #DEFINNING THE FUNCTIONS
connection, cursor = initial_database()
print("delte book")
title = title.get()
author = author.get()
year = year.get()
isbn = isbn.get()
code = removing_from_table(cursor, title, author, year, isbn)
if code==-1:
messagebox.showerror("Error!", "something went wrong!")
messagebox.showinfo("Deleted!", f"Deleted the book: {title}, {author}, {year}, {isbn}")
saving_changes(connection)
terminate_database(connection)


#Graphical user interface creation

#importing
from tkinter import *
from tkinter import ttk
from tkinter import messagebox

def gui(): #DEFINNING THE FUNCTIONS
# header to the interface
root = Tk()
root.title("Library Book Management System")

# the widigets in the dictionary app
title = StringVar()
author = StringVar()
year = StringVar()
isbn = StringVar()

title_label = Label(root, text="TITLE : ", padx=50)
author_label = Label(root, text="AUTHOR : ", padx=10)
year_label = Label(root, text="YEAR : ", padx=10)
isbn_label = Label(root, text="ISBN : ", padx=10)

title_entry = Entry(root, textvariable=title)
author_entry = Entry(root, textvariable=author)
year_entry = Entry(root, textvariable=year)
isbn_entry = Entry(root, textvariable=isbn)


scrollbar = Scrollbar(root, orient="vertical")
listArea = Listbox(root, bg='red', width=200, yscrollcommand=scrollbar.set)

list_all_btn = Button(root, text="List All", command=lambda:listing_all_books(listArea))
search_btn = Button(root, text="Search", command=lambda:searching_books(listArea, title, author, year, isbn))
add_btn = Button(root, text="Add", command=lambda:adding_books(title, author, year, isbn))
del_btn = Button(root, text="delete", command=lambda:deleting_book(title, author, year, isbn))
exit_btn = Button(root, text="Exit", command=root.destroy)

scrollbar.config(command=listArea.yview)

title_label.grid(row=0, column=0)
author_label.grid(row=0, column=2)
year_label.grid(row=0, column=4)
isbn_label.grid(row=0, column=6)


title_entry.grid(row=0, column=1)
author_entry.grid(row=0, column=3)
year_entry.grid(row=0, column=5)
isbn_entry.grid(row=0, column=7)

listArea.grid(row=1, column=0, columnspan=8, sticky="nsew")
scrollbar.grid(row=1, column=9, sticky="ns")

list_all_btn.grid(row=2, column=0)
search_btn.grid(row=2, column=1)
add_btn.grid(row=2, column=2)
del_btn.grid(row=2, column=3)
exit_btn.grid(row=2, column=4)

# looping
print("GUI loop INITIATING")
root.mainloop()
print("GUI loop TERMINATED")

#running the app
gui()

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.