【发布时间】:2020-02-09 12:28:42
【问题描述】:
# all ingredients, represented by numbers: 0= empty selection 1=rice 2=spice 3=vegetable
allIng = [0,1,2,3]
#Each individual recipe(r)
# Veggie Rice Balls
r1 = (0,1,3)
# Curry Rice
r2 =(0,1,2)
# Herb Sauté
r3 = (0,2,3)
# Vegetable Curry
r4 = (1,2,3)
# all recipes on one list
allRec = [r1,r2,r3,r4]
allRecNames = {(0,1,3): 'Veggie Rice Balls', (0,1,2): 'Curry Rice', (0,2,3): 'Herb Sauté', (1,2,3): 'Vegetable Curry'}
#ingredients picked
iP = []
#ingredient count
iC = 1
#User given option to pick up to 3 ingredients
while iC <= 3:
pitem = int (input ("Pick up to 3 items "))
if pitem in allIng:
iP.append(pitem)
print(iP)
iC += 1
else:
print ("Incorrect entry, please pick again")
#sort list
iP.sort()
iP = tuple(iP)
#compare iP to allRec looking for matches
if iP in allRec:
match = set ([iP]) & set(allRec)
print ("Match:",match)
allRecNames[match]
大家好,
试图让我的代码打印出与它们各自匹配的菜肴的名称。例如,如果我输入 0,1,3,我会拿回素食饭团。
当前出现错误:TypeError: unhashable type: 'set'
如果我错了,请纠正我和 ELI5,但这是否意味着我需要先将匹配转换为可散列的东西:
allRecNames[match]
之前有人在我的代码中推荐了元组转换,并认为我也可以在这里做类似的事情,但没有任何痕迹和错误。
与往常一样,如果这是一个愚蠢的帮助,我们将不胜感激。
【问题讨论】:
标签: python python-3.x