简体   繁体   English

Python:添加用户输入的书籍

[英]Python: add a book with user input

I'm trying to add a book to an inventory list, based on user input and get a "str not callable" error?我正在尝试根据用户输入将一本书添加到库存列表中并收到“str not callable”错误?

#Add a book, based on user input 
def add_book():
    # purpose: add a book
    print()
    print("Adding a New Book..")
    print()
        
    title = input("Title> ")
    author = input("Author> ")
    isbn = input("ISBN> ")
    callnumber = input("CallNumber> ")
    stock = input("Stock> ")
    loaned = input("Loaned> ")        
    inventory.append = (title, author, isbn, callnumber, stock, loaned)

I'm not sure how else to ask for input to append the new book to existing ist of books?我不确定如何要求输入 append 新书到现有书籍列表?

That is not the correct syntax to append to a list .这不是 append 到list的正确语法。

Assuming inventory is a list type, the append() method takes a single argument and appends it to the list.假设 inventory 是一个list类型, append()方法接受一个参数并将其附加到列表中。

inventory.append(title)

You will need to do this for each element you wish to append, or append them as a tuple of items您需要为每个您希望 append 或 append 将它们作为项目元组的元素执行此操作

inventory.append((title, author, isbn, callnumber, stock, loaned))

You will first want to adjust your function to allow for a inventory "list" to be passed to it, like so:您首先要调整您的 function 以允许将库存“列表”传递给它,如下所示:

def add_book(inventory):

From here you will be able to append a "tuple" with values separated by commas, like so:从这里您将能够 append 一个“元组”,其值以逗号分隔,如下所示:

inventory.append((title, author, isbn, callnumber, stock, loaned))

Be careful to make sure to have the tuple elements stored in their own parentheses, in addition to the ones from the append method.除了来自 append 方法的元组元素之外,请注意确保将元组元素存储在它们自己的括号中。

You haven't define the inventory .您还没有定义inventory You have to devine above in your code then variable inventory the use it.您必须在您的代码中进行以上设计,然后使用它进行可变库存

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM