[Tutor] function for loaning a book
David L Neil
PyTutor at DancesWithMice.info
Wed Nov 27 23:54:33 EST 2019
On 28/11/19 2:56 AM, samhar zghaier wrote:
> Hello
> I creatred a function to read each of my text files and read them into the
> program, im now trying to delete a certain item from one of the two lists i
> created and add it to the other one
> currently i am not able to add a certain element to the loan list
> This is the code i have currently written
Thanks for showing us your code! (a line or two of book-data may have
helped, but wasn't strictly necessary)
This has the flavor of a homework assignment. Correct?
Some of the answers have been quite complex and offer advanced coding
techniques. Perhaps we should concentrate on 'the basics' first?
When we can't 'see' what's happening 'inside' a Python pgm, the (easy)
'solution' is to add print statements which 'show the working' (as my
math teacher used to say). For example:
> def loan_book():
> book_title = input("What is the name of the book")
print( f"Book name = { book_title }" )
or print( "Book name =", book_title )
# both reveal the same information
> info = book_list
> for line in info:
> if line.title == book_title:
> loan_list.append(line)
print( f"New loan list: { loan_list }" )
These have removed the guess-work. Either they show that the code is
working exactly as you expect - or not!
Another way (a better way?) to approach code for the first time is to
use the Python REPL. On my machine, I open a terminal (?DOS Box on
MS-Win), issue the command "python3" and can then experiment with Python
code, eg:
[dn at JrBrown ~]$ python3
Python 3.7.4 (default, Jul 9 2019, 16:48:28)
[GCC 8.3.1 20190223 (Red Hat 8.3.1-2)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> loan_list = [ 'book1', 'book2' ]
>>> print( loan_list )
['book1', 'book2']
>>> loan_list.append( 'book3' )
>>> print( loan_list )
['book1', 'book2', 'book3']
>>>
Ahah, so that's how to extend a list!
NB in the REPL, using print() is optional, typing only "loan_list" would
produce the same output.
Notice how this snippet 'proves' my understanding of list-manipulation,
without all the other detail, eg pulling the book data from a file.
1 broke the larger problem down into smaller problems
2 focussed on a single detail - solve one (of the smaller) problem at a
time!
Having learned/confirmed the process of extending a (loan_)list, you
could now use the REPL to experiment with the next step: how to remove
the book's entry from the book_list...
Which brings me to ask a design question (cf "coding"): why lists?
If the answer is basically: "this is how the assignment tells me to do
things", then read no further.
When we have 'lists' (the more general, and more pythonic word, is
"collections") which we will later have to "search" (an important ComSc
word), we need to think about how the collection will be "accessed"
(more ComSc).
In this case, the individual books (in the collection) are identified by
some "key", eg the title, author, or accession number, which the user
will input. The code (as is) then searches the list by "sequentially"
(more ComSc!) checking/"searching" each entry in the list against the "key".
An alternate way to design a solution to this problem, having identified
both "collection" and "key", is to use a Python dict (dictionary).
Whereas someone using a list may have based his/her decision upon 'what
the data looks-like', the dict-approach is thinking about how that data
is going to be used! Hopefully you can detect the difference in approach?
In this case, each 'entry' in the collection is logically split into two
parts: the "key" and the "data" (ComSc talks about "independent" and
"dependent" parts, but that is unnecessarily formal), eg
python_coder = dict()
python_coder[ "Samhar" ] = 1
Here, your name is the key, and the value "1" is 'whatever data you want
it to be', eg your position in class. Note that the 'data' part can be
any Python data construct, eg a list():
python_coder = dict()
python_coder[ "sz" ] = [ 'good-looking', 'intelligent', 'happy' ]
python_coder[ "dn" ] = [ 'ugly', 'bottom of the class', 'grumpy' ]
Why bother? Because, now instead of searching (potentially the entire)
list sequentially, we can use the "key" to go straight ("direct access")
to the data:
initials = input( "Who you gonna call?" )
print( f"You called { initials } who is { python_coder[ initials ] }" )
Which is (a) faster to write the code, and (b) faster to execute! (and
probably more effective than calling "Ghostbusters")
That is the essence of data access. Perhaps you were required to use a
list. So do that. In which case, perhaps you are now one-step-ahead when
it comes to the introduction of dict(ionaries)!
As others have observed, there are more advanced concepts we can - or
even "should" consider, eg what if the user enters a 'key' that doesn't
exist in the list/dict? However, I suspect that such fit into a more
advanced course. OTOH I hope this wasn't too simple for you...
--
Regards =dn
More information about the Tutor
mailing list