这个是不同的...我的代码围绕用户输入的信息并使用该信息进行各种操作,例如存储它,对它进行多重复制和添加,以及将其存储为变量。在代码的最后,我希望从用户输入的内容中打印出收据,在dict中翻译成其他名称。 Phew,这是我的代码:
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("That would be, []".format(items[chos_items]))
。当我运行它时,这就是输出的内容
print("That would be, []".format(items[chos_items]))
TypeError: unhashable type: 'list'
我已经尝试将列表更改为trouple但这没有帮助。我对我的生活不知道如何解决它。请帮助,谢谢> _<
答案 0 :(得分:0)
您收到此错误,因为您正在将list
对象作为items
词典的关键字传递。以下是举例说明:
>>> 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'
此外,您需要{}
使用string.format()
代替[]
,否则您的内容不会被添加到字符串中。例如:
>>> 'Hello World []'.format('StackOverflow')
'Hello World []'
>>> 'Hello World {}'.format('StackOverflow')
'Hello World StackOverflow'
答案 1 :(得分:0)
两个主要问题:
1)您将结果存储在chos_items
中,然后不对它们执行任何操作。退出while循环时,full_items包含"end"
或""
print("That would be, {}".format(' '.join([items[int(item)] for item in chos_items])))
将为您提供所选项目的列表,但不会提供多少项目,因为您不会存储该值。
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 item in items:
这将大大简化你的逻辑,因为dict可以完成大部分工作。