[Tutor] function for loaning a book

Alan Gauld alan.gauld at yahoo.co.uk
Wed Nov 27 12:26:17 EST 2019


On 27/11/2019 16:59, Alan Gauld via Tutor wrote:

> Similarly loan_list only exists inside main() so this function cannot
> see it. You need to make loan_list a global variable. Better still pass
> them in as parameters:
> 
> def loan_book(title, books, loans):
>     for book in books:
>         if book.title == title:
>             loans.append(line)

Oops, cut n paste error, that last line should be:

loans.append(book)

Also you could do the whole thing with a list comprehension:

def loan_book(title, books, loans):
    return [book for book in books if book.title == title]



-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list