from collections import namedtuple
Book = namedtuple('Book', 'author title genre year price instock')
BSI = [
Book("JK Rowling", "Harry Potter", "Fantasy", 1997, 10.00, 50),
Book("Harper Lee", "To Kill a Mockingbird", "Fiction", 1960, 15.00, 100),
Book("Dan Brown", "Da Vinci Code", "Thriller", 2003, 20.00, 500),
Book("Mr. Python", "How to Python", "Technology", 2010, 40.00, 10),
Book("Stephen King", "It", "Horror", 1986, 50.00, 10),
Book("Some Guy", "Time Traveling", "Technology", 2020, 800.00, 256)
]
def Book_collection_attribute (BSI: list, attribute: str) -> list:
'''Print out the list of the specific attribute of the list of Book collection'''
for i in BSI:
print(i.attribute)
return BSI
print(Book_collection_attribute(BSI,'title'))
私の目標は、前のリストの属性のリストを出力する汎用関数を作成することです (この例では、本のリストとその属性の 1 つ: タイトル、ジャンル、価格)。Python 3.3でこれを行うことはできますか?
エラーが発生し続ける:
Traceback (most recent call last):
File "C:\Users\ntt2k\Desktop\ICS 31\lab3.py", line 133, in <module>
print(Book_collection_attribute(BSI,'title'))
File "C:\Users\ntt2k\Desktop\ICS 31\lab3.py", line 131, in Book_collection_attribute
print(i.attribute)
AttributeError: 'Book' object has no attribute 'attribute'