将新对象添加到单独变量中的列表中



我对如何将新对象添加到列表有点困惑。我觉得我想得太多了,但如果我在正确的轨道上,请告诉我。

编辑:删除旧代码

明确一点,我不想直接编辑display()中引用的文本文件(然后读取该文件),我想使用append方法将一本新书添加到列表的末尾。我使用分号通过拆分获得对象,所以当我添加新书时,它们是否也应该显示分号以匹配列表的其余部分?

编辑:这是我现在有的。

class Book: 
def __init__(self, title, author, isbn, callnumber, stock, loaned):
self.title = title
self.author = author
self.isbn = isbn
self.callnumber = callnumber
self.stock = stock
self.loaned = loaned
self.available = int(self.stock)-int(self.loaned)
def getTitle(self):
return self.title
def getAuthor(self):
return self.author
def getISBN(self):
return self.isbn
def getCallNumber(self):
return self.callnumber
def getStock(self):
return self.stock
def getLoaned(self):
return self.loaned
def getAvailable(self):
return self.available
def __repr__(self):
return self.title + 't' + self.author + 't' + self.isbn + 't' + self.callnumber + 't' + self.stock + 't' + self.loaned + 'n'
def inventory():
fmtstring = '''{:<50}t{:<20}t{:<13}t{:<13}t{:<10}t{:<10}t{:<10}'''
print(fmtstring.format("Name","Author","ISBN","Call Number","Stock","Loaned","Available"))
books = []
with open("books.txt", "r") as inventoryfile:
for line in inventoryfile:
strip_lines=line.strip()
inventory = strip_lines.split(";")
book = (Book(inventory[0],inventory[1],inventory[2],inventory[3],inventory[4],inventory[5]))
books.append(book)
print(fmtstring.format(*fields(book)))
@staticmethod
def input_book():
title = input("Provide the title of the book> ")
author = input("Provide the author of the book> ")
isbn = input("Provide the ISBN of the book> ")
callnumber = input("Provide the call number of the book> ")
stock = input("Provide the stock of the book> ")
return Book(title, author, isbn, callnumber, stock, 0)

And these are called on with menu options.
while True:
print()
print("Westlands Book Inventory Management Subsystem")
print("1. Display Inventory")
print("2. Add a Book")
print("3. Remove a Book")
print("4. Export Inventory")
print("5. Quit IMS")
choice=eval(input("Select an option from the menu> "))
if(choice==1):
print()
print("Displaying Westlands Books Inventory")
print()
Book.inventory()

elif(choice==2):
print()
print("Adding a Book")
print()
Book.input_book()
print()
print('Book added successfully.')

这是安全"我的代码版本不会破坏任何东西。我试过在不同的地方的追加命令,但似乎不工作。我似乎根本无法返回输入到input_books中的内容。由于

从用户输入创建Book的函数应该只是return而不是Book,而不是试图将其添加到列表(主要是因为:什么列表?)。如果它是Book上的方法,它应该是静态方法(或类方法),而不是实例方法,因为它的目的是创建实例。例如:

@staticmethod
def input_book():
title = input("Provide the title of the book> ")
author = input("Provide the author of the book> ")
isbn = input("Provide the ISBN of the book> ")
callnumber = input("Provide the call number of the book> ")
stock = input("Provide the stock of the book> ")
return Book(title, author, isbn, callnumber, stock, 0)

现在你的Book类之外,你可以:

books = []
while True:
print()
print("Westlands Book Inventory Management Subsystem")
print("1. Display Inventory")
print("2. Add a Book")
choice = input("Select an option from the menu> ")
if choice == "1":
print(books)
if choice == "2":
print()
print("Adding a Book")
print()
books.append(Book.input_book())
print()
print('Book added successfully.')

注意books不是Book,它是Books的list

最新更新