为什么当我尝试创建一个从我的图书馆中删除书籍的功能时,我得到了一个错误?
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:
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
尝试替换:
books_library["Books"][book].pop(book)
作者:
books_library["Books"].pop(book)
基于此:
{'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'}}]}
您正在从 'books_library' 词典中选择 'Books'。
books_library => 字典
books_library['Books'] => 列表
books_library["书籍"][书籍] => 字典
从第一行记起[=13=]
for book in range(len(books_library["Books"]))
'book' 是一个整数,在 books_library["Books"][book].
下的字典中不作为键出现
我觉得应该是这个词
books_library['Books'].pop(book)
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
尝试替换:
books_library["Books"][book].pop(book)
作者:
books_library["Books"].pop(book)
基于此:
{'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'}}]}
您正在从 'books_library' 词典中选择 'Books'。
books_library => 字典
books_library['Books'] => 列表
books_library["书籍"][书籍] => 字典
从第一行记起[=13=]
for book in range(len(books_library["Books"]))
'book' 是一个整数,在 books_library["Books"][book].
下的字典中不作为键出现我觉得应该是这个词
books_library['Books'].pop(book)