[Tutor] function for loaning a book

Alan Gauld alan.gauld at yahoo.co.uk
Wed Nov 27 11:59:53 EST 2019


On 27/11/2019 13:56, samhar zghaier wrote:

> file_name = "text.txt"
> loan_file = "loan.txt"
> 
> class Book():
>     def __init__(self, title, writer):
>         self.title = title
>         self.writer = writer





> def get_book_list():
>     book_list = []
>     with open(file_name, "r") as file:
>         lines = file.readlines()
>         for line in lines:
>             line = line.split(",")
>             book_list.append(Book(line[0].strip(), line[1].strip()))
>     return book_list
> 
> def get_loan_list():
>     loan_list = []
>     with open(loan_file, "r") as file:
>         lines = file.readlines()
>         for line in lines:
>             line = line.split(",")
>             loan_list.append(Book(line[0].strip(), line[1].strip()))
>     return loan_list

Note that these two functions are identical. You only need one, into
which you pass the required file:

def get_list(fileName):
    theList = []
    with open(fileName, "r") as file:
        lines = file.readlines()
        for line in lines:
           line = line.split(",")
           theList.append(Book(line[0].strip(), line[1].strip()))
    return theList

Then you can call it with

loan_list = get_list(loan_file)
book_list = get_list(file_name)

Another tweak would be not to read the file with readlines but just
iterate over the file directly:

    with open(fileName, "r") as file:
        for line in file:
           ...

> def loan_book():
>     book_title = input("What is the name of the book")

It is usually better to place user interaction outside of
the processing functions. You culd read the input then pass
the value into the function.

def loan_book(book_title):
...

>     info = book_list

This tries to assign book_list to info. But book_list is a local
variable declared inside main() and loan_book() cannot see it, so
you should be getting an error here. Yu would need to make
book_list a global variable.

>     for line in info:

line is a terrible choice of name since the list isa list of Books. It
should probably be:

for book in info:

>         if line.title == book_title:
>             loan_list.append(line)

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)
> def main():
>     loan_list = get_loan_list()
>     book_list = get_book_list()
      book_title = input(("What is the name of the book")
>     loan_book(book_title, book_list, loan_list)

Finally, how do you know whether this works or not?
You never print any output.

I'd suggest adding the following at the end of main():

print("books: ", book_list)
print("loans: ", loan_list)

To see if the book has in fact transferred.


-- 
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