Я хочу разработать базовую систему управления библиотекой (lms), которая позволяет пользователю добавлять несколько книг и сохранять их в словаре, где book_name - это ключ, а список других атрибутов (author_name, publishing_name ...) - это значение для соответствующего ключа. Но после добавления первой книги, когда я добавляю вторую книгу, детали первой книги перезаписываются второй книгой, что не так, как я хочу. Использование метода update () для обновления «book_dict» также не помогло. Могу ли я получить решение этой проблемы? Вот код вместе с выводом
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: "))
Выход:
************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
*****************************************