我對如何將新物件附加到串列有點困惑。我覺得我想多了,但如果我在這里走對了,請告訴我。
編輯:洗掉舊代碼
需要明確的是,我不想直接編輯 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.')
這是我的代碼的“安全”版本,不會破壞任何東西。我已經在各個地方嘗試了 append 命令,但似乎沒有用。我似乎根本無法回傳輸入到 input_books 中的內容。謝謝
uj5u.com熱心網友回復:
Book您從用戶輸入創建的函式應該只是return那樣,Book而不是嘗試將其添加到串列中(主要是因為:什么串列?)。如果它是 on 的方法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不是 a Book,而是slist的Booka 。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/494925.html
