Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why when I trying to make a function that delete books from my library I get an Error?

Example of book that I want to delete from the books_library (depends on the book name (input)):

{‘Books’: [{"Book’s ID": {‘001’}, "Book’s Name": {‘Avengers’}, "Book’s Authors": {‘Stan Lee’}, "Book’s Published year": {‘1938’}, "Book’s Type": {‘1′}, "Book’s Copies": {’10’}}, {"Book’s ID": {‘002’}, "Book’s Name": {‘Spider Man’}, "Book’s Authors": {‘Stan Lee’}, "Book’s Published year": {‘1948’}, "Book’s Type": {‘2′}, "Book’s Copies": {’15’}}]}

The function i’m using:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

def delete_book(book_name, books_library):
"""
A function that update the collection of books that in the library, without deleting one.
:param book_name: The name of the book to delete.
:param books_library: A collection of all the books in library
"""

for book in range(len(books_library["Books"])):
    if book_name in books_library["Books"][book]["Book's Name"]:
        print("Are you sure you want delete this book?: ")
        identifier = input()
        identifiers = ['y','n']
        while identifier not in identifiers:
            print("Please answer with y or n")
            print("Are you sure you want to delete this book?: ")
            identifier = input()
        if identifier == 'y':
            books_library["Books"][book].pop(book)
            return f"{book_name} is deleted from books library"
        ## Todo : Find a way to delete book by his name (should delete all books details)
        else:
            print(f"The book {book_name} does not exist!")
        if identifier == 'n':
            print("Canceling...")
            break

The code i’m using in Main.py:

        if identifier == '4':  # choosing to Remove a book
           print("Which book would you like to remove?: \n")
           book_name = input()
           delete_book(book_name, books_library)
           print("The Book has deleted from library.")

The Error that I get every time:

Traceback (most recent call last):
  File "d:\John bryce - python\FirstProject-managing book library\main.py", line 83, in 
  <module>
  delete_book(book_name, books_library)
   File "d:\John bryce - python\FirstProject-managing book library\LibraryBooks.py", 
   line 91, in delete_book
   books_library["Books"][book].pop(book)
   KeyError: 0

>Solution :

Try to replace:

books_library["Books"][book].pop(book)

By:

books_library["Books"].pop(book)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading