繁体   English   中英

如何在Python中将新的一组键值对添加到字典中?

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

我希望开发一个基本的图书馆管理系统(lms),该系统允许用户添加多本书并将它们存储在字典中,其中book_name是键,而其他属性(author_name,publication_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
*****************************************

这应该工作。 我发现的错误是您使用了错误的数据结构来存储数据,如果我理解正确的话,那么您将在每个用户输入上重新初始化它。 此更新应该能够容纳更多的书,并以更有条理的方式相应地显示它们。

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()

欢迎来到SO,亲爱的朋友! 首先,您应该缩进代码,其次,您的问题是您应该将book变量设置为global。 希望这段代码对您有所帮助!

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: "))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM