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

Time:12-23

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:

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

CodePudding user response:

Try to replace:

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

By:

books_library["Books"].pop(book)

CodePudding user response:

Based on this:

{'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'}}]}

You're selecting 'Books' from 'books_library' dictionary.

books_library => dict

books_library['Books'] => list

books_library["Books"][book] => dict

Remember from the first line

for book in range(len(books_library["Books"]))

'book' is an integer and it's not present as a key in the dict under books_library["Books"][book].

I think this should word

books_library['Books'].pop(book)
  • Related