我有一个函数,它获取一个图书列表,并返回每本图书的一个大字符串,后跟一个换行符。你知道吗
Book = namedtuple('Book', 'author title genre year price instock')
Book('Suzane Collins','The Hunger Games', 'Fiction', 2008, 6.96, 20),
Book('J.K. Rowling', "Harry Potter and the Sorcerer's Stone", 'Fantasy', 1997, 4.78, 12)
我做了以下功能:
def Booklist_display(dlist):
for item in dlist:
return '{name} {price} {stock}'.format(name=item.name, price=item.price, stock=item.instock)
但它只印第一本书,不印第二本书。你知道吗
Suzane Collins 6.96 20
有人能帮我理解我的代码是否正确,为什么我的函数只打印第一部分?我似乎不能确定逻辑。你知道吗
在这个
for
循环中:当循环在第一个对象上迭代时,函数退出(因为
return
)。你知道吗将结果存储在列表中,稍后返回:
您可以将
join
与列表一起使用。你知道吗相关问题 更多 >
编程相关推荐