Hi. I’m trying to make a library class where you can look up all the books based on title, author, date, etc. However, it only returns one title when I run the code. I want it to display all books that have a trait. Any idea how I can fix this. (This is on Python. It is homework for an enrichment course.) I don’t know if the link will work. https://replit.com/@EricZhang75/HW-1#main.py
#Python Project Help
1 messages · Page 1 of 1 (latest)
Okay so in all of your get methods : getbookbyauthor, getbookbytitle, and getbookbyyear, the code is printing and returning as soon as it finds the first book that matches its search criteria.
To fix this you will need to return True or False outside of the for loop. Here is an example:
def getbookbytitle(self, title):
found = false
for book in self.collection:
if book.title == title:
print(book.title + ", " + book.author + ", " + str(book.year))
found = true
return found