[Tutor] function for loaning a book
Sam
samharzghaier at gmail.com
Wed Nov 27 11:49:06 EST 2019
Oh now I understand, wo the value of the book I loaned Is added to the list within the program but never written to my text file.
Thank you for your help
Sent from Mail for Windows 10
From: Mats Wichmann
Sent: Wednesday, 27 November 2019 17:46
To: tutor at python.org
Subject: Re: [Tutor] function for loaning a book
On 11/27/19 9:30 AM, samhar zghaier wrote:
> Ok
> The code doesn't move the file to the loan list
> here is what my text file looks like:
>
> book of y, sma
> book of d, adam
> book of f, blag
> book of b, sam
at the most basic level, you don't do anything with the results of the
operation.
>>> This is the code i have currently written
>>>
>>> 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
>>>
>>> def loan_book():
>>> book_title = input("What is the name of the book")
>>> info = book_list
>>> for line in info:
>>> if line.title == book_title:
>>> loan_list.append(line)
and? your local copy of loan_list (see Note on that which follows) has
the line appended... but that is not saved back to your loan file so
it's just lost once the program quits.
>>> def main():
>>> loan_list = get_loan_list()
>>> book_list = get_book_list()
>>> loan_book()
and here: how can you tell what changed? you don't do anything with the
updated list so it's not in the file, and you don't print any results
here either, so there's no way to tell if anything changed.
Note: you obtain loan_list and book_list, but you don't pass them to
loan_book, so you should actually be getting an error in loan_book.
they're not globals...
>>> main()
there are other comments to make, but let's keep it to just the question.
_______________________________________________
Tutor maillist - Tutor at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list