i have function takes list of books , returns 1 large string of each book followed newline character.
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 , sorcerer's stone", 'fantasy', 1997, 4.78, 12) i made following function:
def booklist_display(dlist): item in dlist: return '{name} {price} {stock}'.format(name=item.name, price=item.price, stock=item.instock) but prints first book , not second book.
suzane collins 6.96 20 can me understand if have code correct , why function prints first part only? can't seem pin point logic.
in for loop:
for item in dlist: return '{name} {price} {stock}'.format(name=item.name, price=item.price, stock=item.instock) the function exits when loop iterates on first object (because of return).
store result in list , return later:
strlist = [] item in dlist: strlist.append('{name} {price} {stock}'.format(name=item.name, price=item.price, stock=item.instock)) return '\n'.join(strlist)
Comments
Post a Comment