Python 用列表附加到词典的

Python 用列表附加到词典的,python,dictionary,Python,Dictionary,我有一本以下格式的词典: {'Dickens,Charles': [['Hard Times', '7', '27.00']], 'Shakespeare,William': [['Rome And Juliet', '5', '5.99'], ['Macbeth', '3', '7.99']]} 我想通过询问用户作者的姓和名来添加到这本词典中,这是关键,然后是书名、数量、价格。如果作者已经存在,它应该创建另一个列表列表 def displayInv

我有一本以下格式的词典:

{'Dickens,Charles': [['Hard Times', '7', '27.00']],
 'Shakespeare,William': [['Rome And Juliet', '5', '5.99'],
                     ['Macbeth', '3', '7.99']]}
我想通过询问用户作者的姓和名来添加到这本词典中,这是关键,然后是书名、数量、价格。如果作者已经存在,它应该创建另一个列表列表

def displayInventory(theInventory):
    for names, books in sorted(theInventory.items()):
        for title, qty, price in sorted(books):
            print("Author: {0}".format("".join(names)))
            print("Title: {0}".format(title))
            print("Qty: {0}".format(qty))
            print("Price: {0}".format(price))
            print()


def addBook(theInventory):
    my_list = []
    hello = True
    flag = True
    first = input("Enter the first name of the author: ")
    last = input("Enter the last name of the author: ")
    last = last[0].upper() + last[1:].lower()
    first = first[0].upper() + first[1:].lower()
    author = last + "," + first
    book = input("Enter the title of the book: ")
    book = book.lower()
    book = book.title()
    j = 0

 if author not in theInventory:
    while flag:
        try:
            qty = int(input("Enter the qty: "))
            price = input("Enter the price: ")
            my_list.append(str(book))
            my_list.append(str(qty))
            my_list.append(str(price))
            theInventory[author] = my_list
            flag = False
        except ValueError:
            ("no")

 else:
      for i in theInventory[author][j]:
      if theInventory[author][j][0] == book:
          print("The title is already in the Inventory")
          hello = False

          while flag:
              qty = int(input("Enter the qty: "))
              if qty > 0:
                  flag = False
                  tree = True
                    while tree:
                        price = input("Enter the price: ")
                        my_list.append(str(book))
                        my_list.append(str(qty))
                        my_list.append(str(price))
                        theInventory[author].append(my_list)
                        j+=1
                        tree = False
更新后,当我试图从main打印Inventory时,这会不断抛出错误

File "practice.py", line 270, in <module>
main()
File "practice.py", line 254, in main
 displayInventory(theInventory)
File "practice.py", line 60, in displayInventory
for title, qty, price in sorted(books):
ValueError: need more than 1 value to unpack
文件“practice.py”,第270行,在
main()
文件“practice.py”,第254行,主
显示库存(库存)
displayInventory中第60行的文件“practice.py”
对于分类(书籍)中的标题、数量、价格:
ValueError:需要超过1个值才能解包

如果它的作者以前确实存在,那么它根本不会更新字典,并且在添加了一个具有值的新作者之后,它会抛出错误

回溯的最后三行非常有用:

File "practice.py", line 60, in displayInventory
for title, qty, price in sorted(books):
ValueError: need more than 1 value to unpack
错误消息表明
书籍
不包含您认为它的功能(列表列表)。回溯显示此处发生的错误:

for title, qty, price in sorted(books): # <= ValueError: need more than 1 value to unpack
    print("Author: {0}".format("".join(names)))
    ... # the rest of the loop
编辑:

错误的根本原因如下:

theInventory[author] = my_list
由于
投资项目
应该包含列表列表,因此将赋值更改为

theInventory[author] = [my_list]  

它不断抛出什么错误?@user3599753了解一个关于错误跟踪的想法-使用它们。从阅读它们开始,它包含了问题的答案。如果您不理解它,并且出现堆栈溢出,请在不被反复询问的情况下向我们显示错误跟踪。另外,请格式化您的代码。缩进在python中有点重要。作者的价值来自哪里?不,我有其他功能,更新价格等等,它们工作得很好。这只是增加了一整本书,这就是问题所在
theInventory[author] = [my_list]