I wish to develop a basic library management system (lms) which allows the user to add multiple books and store them in a dictionary where book_name is the key and a list of other attributes(author_name, publication_name ...) is the value for the corresponding key. But after adding the first book, when I go onto adding a second book, the details of the first book are overwritten by the second book which is not the way I want. using update() method to update the "book_dict" also did not help. Can I get the solution for this problem? Here is the code along with the output
def addBook():
book_dict = {}
book_list = []
book_name = input("Enter the book name: ")
book_list.append(book_name)
author_name = input("Enter the author name: ")
book_list.append(author_name)
publication_name = input("Enter the publication: ")
book_list.append(publication_name)
publication_year = input("Enter the year of publication year: ")
book_list.append(publication_year)
cost = input("Enter the cost: ")
book_list.append(cost)
book_dict.update({book_name:book_list})
print(book_dict)
return book_dict
#Ignore the display() method
'''def displayBook(books):
print('''************MENU********************
1. Add a book
2. Display a book with a particular name
3. Quit
*****************************************''')'''
choice = int(input("Enter your choice: "))
while choice != 3:
books = {}
if choice == 1:
books.update(addBook())
print(books)
#elif choice == 2:
# displayBook(books)
elif choice == 3:
exit()
print('''************MENU********************
1. Add a book
2. Display a book with a particular name
3. Quit
*****************************************''')
choice = int(input("Enter your choice: "))
Output:
************MENU********************
1. Add a book
2. Display a book with a particular name
3. Quit
*****************************************
Enter your choice: 1
Enter the book name: a
Enter the author name: a
Enter the publication: a
Enter the year of publication year: 1
Enter the cost: 1
{'a': ['a', 'a', 'a', '1', '1']}
{'a': ['a', 'a', 'a', '1', '1']}
************MENU********************
1. Add a book
2. Display a book with a particular name
3. Quit
*****************************************
Enter your choice: 1
Enter the book name: b
Enter the author name: b
Enter the publication: b
Enter the year of publication year: 2
Enter the cost: 2
{'b': ['b', 'b', 'b', '2', '2']}
{'b': ['b', 'b', 'b', '2', '2']}
************MENU********************
1. Add a book
2. Display a book with a particular name
3. Quit
*****************************************
Welcome to SO, My dear friend! Firstly you should indent your code, secondly, your problem is you should set book variable as global. I hope this code helps!