I'm currently working on a small project to practice (as you'll see from my code, I'm a beginner). The goal is simply to create a mini online library where I can add, delete, and view my books, and check if I already have a particular book in my library.
I know this isn’t the best solution and that I should be using SQL, but this is actually my second version—I wanted to practice working with .json files first.
So everything works, but I wanted to know if you have any advice on how to improve my code and make the syntax cleaner. I also realize I’m not very good at writing functions—can I create them here?
// Source - https://stackoverflow.com/q/79921609
// Posted by Hugo
// Retrieved 2026-04-07, License - CC BY-SA 4.0
import json
import os
import tkinter as tk
file = os.path.dirname(__file__)
biblio_path = os.path.join(file, "bibliotheque.json")
library = []
with open(biblio_path, 'r') as f:
library = json.load(f)
while True:
choice = input("Welcome to your online library. Would you like to (1) add a new book, (2) delete a book, (3) view your library, or (4) search for a book? ")
if choice=="1":
biblio = {}
add_book = input("Which is the tittle of your new book : ")
add_author = input("Who is the author : ")
biblio["book"] = add_book
biblio["author"] = add_author
library.append(biblio)
print(library)
with open(biblio_path, "w") as f:
json.dump(library, f)
if choice =="2":
author_name = input("Who is the author : ")
book_name = input("Which is the tittle of the book : ")
book_to_remove = {}
book_to_remove["book"] = book_name
book_to_remove["author"] = author_name
library.remove(book_to_remove)
print(library)
with open(biblio_path, "w") as f:
json.dump(library, f)
if choice=="3":
window = tk.Tk()
label = tk.Label(window, text=library)
bouton = tk.Button(window, text="Quit", fg="red", command=window.destroy)
label.pack()
bouton.pack()
if choice=="4":
author_searched = input("Who is it author : ")
book_searched = input("Which is the tittle of the book you are looking for : ")
booksearched = {"book": book_searched, "author": author_searched}
if booksearched in library:
print("You have this book")
else:
print("You don't have this book")
Your code lacks function encapsulation, and GUI handling is mixed with logic. It's also repetitive in prompting for author and book details.
def add_book(library):
title = input('Which is the tittle of your new book: ')
author = input('Who is the author: ')
library.append({'book': title, 'author': author})
def delete_book(library):
author_name = input('Who is the author: ')
book_title = input('Which is the tittle of the book: ')
for book in library:
if book['book'] == book_title and book['author'] == author_name:
library.remove(book)
def view_library(library):
window = tk.Tk()
label = tk.Label(window, text=str(library))
bouton = tk.Button(window, text='Quit', fg='red', command=window.destroy)
label.pack()
bouton.pack()
def search_book(library):
author_searched = input('Who is it the author: ')
book_searched = input('Which is the tittle of the book you are looking for: ')
found = False
for book in library:
if book['book'] == book_searched and book['author'] == author_searched:
print('You have this book')
found = True
break
if not found:
print('You don''t have this book')