I am trying to make a Library Management System by using OOPs in python. Here's the codeblock by which I am getting the error:
class Library:
def __init__(self, dictofbooks, library_name):
self.bookdict = dictofbooks
self.namelib = library_name
self.lenddict = {}
def displaybook(self):
print(f"Booklist of {self.namelib}:\n")
for key, value in self.bookdict:
print(f"Name: {key} ||------------|| Author: {value}")
if __name__ == '__main__':
dictofbooks = {"Computer Science C class XI":"Sumita Arora", "Simple Science Experiments":"Minerva Publications", "A Brief History of Time":"Stephen Hawking", "Awareness Science class VIII":"Lakhmir Singh and Manjit Kaur", "Foundation Science Physics class VIII":"Pearson", "Mathematics for class VIII":"R.S. Aggarwal"}
myLib = Library(dictofbooks, "K. Anand Library")
FULL CODE:
class Library:
def __init__(self, dictofbooks, library_name):
self.bookdict = dictofbooks
self.namelib = library_name
self.lenddict = {}
def displaybook(self):
print(f"Booklist of {self.namelib}:\n")
for key, value in self.bookdict:
print(f"Name: {key} ||------------|| Author: {value}")
def lendbook(self, user, book):
if book not in self.lenddict.keys():
self.lenddict.update({book:user})
print("Booklist has been updated. You can take the book now")
else:
print(f"Book is already being lent by {self.lenddict[user]}")
def addbook(self, book, author):
self.bookdict.update({book:author})
print("Book has been added to the booklist\n")
Library.displaybook()
def returnbook(self, book):
self.booklist.remove(book)
if __name__ == '__main__':
dictofbooks = {"Computer Science C class XI":"Sumita Arora", "Simple Science Experiments":"Minerva Publications", "A Brief History of Time":"Stephen Hawking", "Awareness Science class VIII":"Lakhmir Singh and Manjit Kaur", "Foundation Science Physics class VIII":"Pearson", "Mathematics for class VIII":"R.S. Aggarwal"}
myLib = Library(dictofbooks, "K. Anand Library")
while(True):
print(f"Welcome to {myLib.namelib}. Enter your choice to continue:")
print("1. Display Books")
print("2. Lend Books")
print("3. Donate Books")
print("4. Return Books")
userchocie = int(input("--> "))
match userchocie:
case 1:
myLib.displaybook()
case 2:
myLib.lendbook()
case 3:
myLib.addbook()
case 4:
myLib.returnbook()
I tried to display the books and the authors but I got an error. What I did:
class Library:
def __init__(self, dictofbooks, library_name):
self.bookdict = dictofbooks
self.namelib = library_name
self.lenddict = {}
def displaybook(self):
print(f"Booklist of {self.namelib}:\n")
for key, value in self.bookdict:
print(f"Name: {key} ||------------|| Author: {value}")
What I got:
Welcome to K. Anand Library. Enter your choice to continue:
1. Display Books
2. Lend Books
3. Donate Books
4. Return Books
--> 1
Booklist of K. Anand Library:
Traceback (most recent call last):
File "c:\Users\consu\Desktop\Krishna Super\Python Projects [mini to mega]\LibraryManagementSystem\MyLibrary.py", line 43, in <module>
myLib.displaybook()
File "c:\Users\consu\Desktop\Krishna Super\Python Projects [mini to mega]\LibraryManagementSystem\MyLibrary.py", line 9, in displaybook
for key, value in self.bookdict:
^^^^^^^^^^
ValueError: too many values to unpack (expected 2)
PS C:\WINDOWS\System32\WindowsPowerShell\v1.0>
CodePudding user response:
You need to be using self.bookdict.items()
instead of self.bookdict
.
Fixed code is -
def lendbook(self, user, book):
if book not in self.lenddict.keys():
self.lenddict.update({book:user})
print("Booklist has been updated. You can take the book now")
else:
print(f"Book is already being lent by {self.lenddict[user]}")
CodePudding user response:
When iterating through dicts in python you aren't looking to iterate through the key and value at the same time, the only thing you're iterating is the keys in the dict
for key in self.bookdict:
print(f"Name: {key} ||------------|| Author: {self.bookdict.get(key)}")