附加到带有列表的字典

时间:2014-05-06 18:06:15

标签: python dictionary

我有以下格式的字典:

{'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打印出库存时,这会导致错误。

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

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

1 个答案:

答案 0 :(得分:0)

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

File "practice.py", line 60, in displayInventory
for title, qty, price in sorted(books):
ValueError: need more than 1 value to unpack

错误消息表明books不包含您的想法(列表列表)。回溯显示此处发生的错误:

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

在try / except中包装此循环以查看books真正包含的内容。

try:
    for title, qty, price in sorted(books):
        print("Author: {0}".format("".join(names)))
        ... # the rest of the loop

except ValueError:
    print(books)
    raise

修改

错误的根本原因在于:

theInventory[author] = my_list

由于theInventory应包含列表列表,因此将分配更改为

theInventory[author] = [my_list]