Python:不可修复的错误,列表
这个不一样。。。我的代码围绕用户输入的信息工作,并使用这些信息执行各种操作,例如存储信息、对其进行复制和添加,以及将其存储为变量。在代码的末尾,我希望根据用户输入的内容打印一张收据,翻译成另一个名字。呸,这是我的代码:Python:不可修复的错误,列表,python,list,typeerror,Python,List,Typeerror,这个不一样。。。我的代码围绕用户输入的信息工作,并使用这些信息执行各种操作,例如存储信息、对其进行复制和添加,以及将其存储为变量。在代码的末尾,我希望根据用户输入的内容打印一张收据,翻译成另一个名字。呸,这是我的代码: print("Hi There! Welcome to sSpecialists!") print("To start shopping, note down what you want to buy and how much of it") print("Here are th
print("Hi There! Welcome to sSpecialists!")
print("To start shopping, note down what you want to buy and how much of it")
print("Here are the purchasable items")
print("~~~~~")
print("12345670 is a hammer (£4.50)")
print("87654325 is a screw driver (£4.20)")
print("96385272 is a pack of 5 iron screws (£1.20)")
print("74185290 is pack of 20 100mm bolts (£1.99)")
print("85296374 is a pack of 6 walkers crisps (£1)")
print("85274198 is haribo pack (£1)")
print("78945616 is milk (£0.88)")
print("13246570 is a bottle of evian water (£0.99)")
print("31264570 is kitkat original (£0.50)")
print("91537843 is a cadbury bar (£1)")
print("~~~~~")
items = {12345670 : 'hammer',
87654325 : 'screwDriver',
96385272 : 'packOf5IronnScrews',
74185290 : 'packOf200mmBolts',
85296374 : 'packOf6WalkersCrisps',
85274198 : 'hariboPack',
78945616 : 'milk',
13246570 : 'bottleOfEvianWater',
31264570 : 'kitkatOriginal',
91537843 : 'cadburyBar'}
print("Alright, now start typing what you want to order")
print(" ")
subtotal = 0
full_list = " "
chos_items = []
while full_list != "":
print(" ")
full_list = input("Type: ")
if full_list == 'end':
break
amount = int(input("Amount: "))
item = int(full_list)
if item in items:
print("That would be {} {}(s)".format(amount, items[item]))
if full_list == '12345670':
price = (4.50 * amount)
print("Added Hammer(s)")
print("Added "+str(price))
subtotal = subtotal + price
if full_list == '87654325':
price = (4.20 * amount)
subtotal = subtotal + price
print("Added Screw Driver(s)")
print("Added "+str(price))
if full_list == '96385272':
price = (1.20 * amount)
subtotal = subtotal + price
print("Added Pack of 5 iron
print("Added "+str(price))
if full_list == '74185290':
price = (1.99 * amount)
subtotal = subtotal + price
print("Added Pack of 20 100mm bolts")
print("Added "+str(price))
if full_list == '85296374':
price = (1.00 * amount)
subtotal = subtotal + price
print("Added Pack of 6 Walkers crisps")
print("Added "+str(price))
if full_list == '85274198':
price = (1.00 * amount)
subtotal = subtotal + price
print("Added Haribo pack(s)")
print("Added "+str(price))
if full_list == '78945616':
price = (0.88 * amount)
subtotal = subtotal + price
print("Added bottle(s) of milk")
print("Added "+str(price))
if full_list == '13246570':
price = (0.99 * amount)
subtotal = subtotal + price
print("Added bottle(s) Evian water")
print("Added "+str(price))
if full_list == '31264570':
price = (0.50 * amount)
subtotal = subtotal + price
print("Added bar(s) of Kitkat original")
print("Added "+str(price))
if full_list == '91537843':
price = (0.50 * amount)
print("Added Cadbury bar(s)")
print("Added "+str(price))
if full_list != "":
chos_items.append(full_list)
total = round(subtotal)
print("Your subtotal is " +str(total))
print(" ")
print("That would be, []".format(items[full_list]))
print(" ")
print("Your recipt is")
print(" ")
我的代码是一堆类似的东西,但疯狂背后有一种方法。我相信问题发生在print(“也就是说,[])格式(items[chos_items])
。当我运行这个时,这就是输出的内容
print("That would be, []".format(items[chos_items]))
TypeError: unhashable type: 'list'
我试着把清单变成一个麻烦,但没有用。我一辈子都不知道如何修复它。请帮助,谢谢>\u您收到此错误,因为在中,您正在传递
列表
对象作为项
指令的键。下面的示例说明了这一点:
>>> my_dict = {'a': 123, 'b': 234}
>>> my_dict[[]] # accessing `my_dict` with `[]` as value
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
两个主要问题:
1) 您将结果存储在chos_items
中,然后不对其进行任何操作。退出while循环时,完整项包含“end”
或“
将为您提供一个所选所有项目的列表,但不会给出数量,因为您不存储该值
2) 您没有充分利用python dicts:
if item in items:
您可以检查该项是否作为字典存在于项中,但不要使用字典
items = {'12345670' : {'name' : 'Hammer', 'price' : 4.50},
'87654325' : {'name' : 'screwDriver', 'price' : 4.20}}
如果这是您的词典,您可以执行以下操作:
for item in items:
print("{} is a {} (£{:0.2f})".format(item, items[item]['name'], items[item]['price']))
将为您打印项目列表及其价格:
87654325 is a screwDriver (£4.20)
12345670 is a Hammer (£4.50)
由于item
是数字,item['name']
是名称,item['price']
是价格。然后,您可以将if/if/if/if
块合并到单个查找中:if项在项中:
这将大大简化您的逻辑,因为dict完成了大部分工作。只是检查是否正确,但您要查找的语法不是花括号,不是方括号吗<代码>{}不是
[]
?它应该是打印的(“那将是,{}”)print(“那将是,{}”).format(项目[chou项目])
我还没有看过你的代码,但是你需要阅读if/elif/else
,而不是if
(必须测试每个条件)这是它的第二稿,但是我试着用一个列表来索引字典-列表不是有效的dict键,因为它们是不可散列的。您的字符串格式还有其他问题如何解决?进一步解释对不起,我只是在高中它的作品,但是,它只显示最近输入的number@FlagShipKILLER我鼓励你再尝试一下,看看如何使用字典的教程。他们非常强大。这是一个故意的不完整的解决方案,所以你必须努力使它工作。我理解,谢谢,但认真的。最后一个问题是,如何使输出仅为小数点后2位{:0.2f}
告诉格式化程序在小数点后有.2
2位数字,并且它应该期望有f
浮点。
for item in items:
print("{} is a {} (£{:0.2f})".format(item, items[item]['name'], items[item]['price']))
87654325 is a screwDriver (£4.20)
12345670 is a Hammer (£4.50)