I have a database saved as a list of tuples, and every tuple has 4 elements. The first element is the book’s number, the second element is the book’s name, the third element is the book’s author, and the forth element is the book’s publishing year.
I need a function called findBook(L, keyword), that receives the books as a list of tuples, keyword as a string, and returns all the tuples of books that have keyword in them.
The tuple of the book should be like this:
(BookNumber, BookName, Author, YearPublished)
How to write a search engine that will help us find these certain books?
I’ve tried to start with this, but it isn’t working:
def findBook(L,keyword):
for i in L:
BookNumber=i[0]
BookName=i[1]
Author=i[2]
YearPublished=i[3]
i=(BookNumber,BookName,Author,YearPublished)
if keyword == str(BookName) or keyword==BookNumber or keyword==str(Author) or keyword==str(YearPublished):
return i
so if i have a harry potter book , and i enter the keyword : “harry” it should return all the whole tuple.
You could iterate over the entries that may have the
keywordyou’re looking for, grouping them into a list, that shall be returned by the end of the process:Output:
Notice the third entry was not found because the
keywordis “harry” and not “Harry”, to do insensitive case search, use: