为啥当我尝试创建一个从我的图书馆中删除书籍的功能时会出现错误?

Posted

技术标签:

【中文标题】为啥当我尝试创建一个从我的图书馆中删除书籍的功能时会出现错误?【英文标题】:Why when I trying to make a function that delete books from my library I get an Error?为什么当我尝试创建一个从我的图书馆中删除书籍的功能时会出现错误? 【发布时间】:2022-01-24 01:53:12 【问题描述】:

我想从 books_library 中删除的图书示例(取决于书名(输入)):

'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 ',“书的作者”:'Stan Lee',“书的出版年份”:'1948',“书的类型”:'2',“书的副本”:'15']

我正在使用的功能:

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

我在 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.")

我每次遇到的错误:

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_library = "Books": [] @Programmin-in-Python 【参考方案1】:

基于此:

'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_library => 字典

books_library['Books'] => 列表

books_library["Books"][book] => 字典

记住第一行

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

'book' 是一个整数,它不作为键出现在 books_library["Books"][book] 下的字典中。

我觉得应该这样说

books_library['Books'].pop(book)

【讨论】:

【参考方案2】:

尝试替换:

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

作者:

books_library["Books"].pop(book)

【讨论】:

谢谢我的朋友!,这是工作你能解释一下这两个代码行之间的区别吗? @Corralien books_library["Books"] 是您的列表。 pop 方法附加到您的列表而不是里面的子字典。 type(books_library["Books"][book]) 返回dicttype(books_library["Books"]) 返回list。你更清楚了吗? 是的,现在更清楚了。谢谢

以上是关于为啥当我尝试创建一个从我的图书馆中删除书籍的功能时会出现错误?的主要内容,如果未能解决你的问题,请参考以下文章

当我尝试加载部分视图时,为啥会加载新页面?

当我尝试从单链表中删除节点时,为啥我的 C 程序会崩溃

作业搜索书籍[重复]

Java图书管理系统

为啥你认为当我从我的 CF 卡上传图像到我的电脑时,有些图像会随机损坏?

功能文件指向不存在的项目