I have a Class Library, inside which I have declared a method borrowBook(self, bookname). Whenever I call this method it successfully displays the name of the first book which is at index 0 in the list - availablebooks, but whenever I enter a bookname which is at index other than 0 then it becomes unable to find that book and prints the else part!
class Library:
.
.
.
def borrowBook(self, bookname):
for book in range(len(availablebooks)):
if bookname in availablebooks[book]['Title']:
print("Here is your book! Enjoy")
availablebooks.pop(book)
return True
else:
print("The book is not available")
return False
.
.
.
availablebooks = []
.
.
.
choice = int(input("Enter your choice (1/2/3): "))
if choice == 1:
library.displayAvailableBooks()
elif choice == 2:
library.borrowBook('Eloquent JavaScript, Third Edition')
The availablebooks list looks like this:
availablebooks = [{'Title': 'Eloquent JavaScript, Third Edition', 'Author': 'Marijn Haverbeke'}], [{'Title': 'Practical Modern JavaScript', 'Author': 'Nicolás Bevacqua'}]
The book name in the borrowBook is at the first index of the list and it displays the desired result, but...
elif choice == 2:
library.borrowBook('Practical Modern JavaScript')
This book is at index 1 and this time instead it skips to the else part of the method where it says: "The book is not available"
Help me to solve this. I am stuck! Thank You!