How to append a new set of key,value pair to a dictionary in Python?

834 views Asked by At

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
*****************************************
2

There are 2 answers

1
Mark White On BEST ANSWER

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!

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


choice = int(input("Enter your choice: "))
books = {}

while choice != 3:
    if choice == 1:
        books = addBook(books)
        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: "))
2
Zeeshan Sultan On

This should work. The mistake i found was you were using the wrong data structure to store your data also you were re-initializing it on every user input if i got this right. This update should be able to hold more books and display them accordingly in a more organized manner.

book_list = []

def addBook():
    book_dict = {}
    book_name = raw_input("Enter the book name: ")
    book_dict["Name"] = book_name
    author_name = raw_input("Enter the author name: ")
    book_dict["Author"] = author_name
    publication_name = raw_input("Enter the publication: ")
    book_dict["Publication"] = publication_name
    publication_year = raw_input("Enter the year of publication year: ")
    book_dict["Year"] = publication_year
    cost = raw_input("Enter the cost: ")
    book_dict["Cost"] = cost
    book_list.append(book_dict)        
    return True


def choose():
    print('''************MENU********************
    1. Add a book
    2. Display a book with a particular name
    3. Quit
    *****************************************''')
    choice = int(input("Enter your choice: "))
    if choice == 1:
        addBook() 
        print(book_list)
    #elif choice == 2:
    #   displayBook(books)
    elif choice == 3:
        exit()



while True:
    choose()