Python “匹配索引并返回次索引”;“元素”;
几天前,一些人帮我完成了一些非常基本的代码,以便将配料与各自的食谱相匹配:Python “匹配索引并返回次索引”;“元素”;,python,python-3.x,Python,Python 3.x,几天前,一些人帮我完成了一些非常基本的代码,以便将配料与各自的食谱相匹配: # 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é r
# 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]
#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)
有没有关于哪种方式更有效的建议,以及如何获得所需的蔬菜咖喱、香草沙司等印刷品的想法。?一如既往,感谢您抽出时间。如果标题没有意义,很抱歉,大约两周前,几年来第一次重新开始玩代码 您可以按照以下方式创建食谱及其名称的字典,因为食谱是元组,不可变的
recipe_names = {(0,1,2): 'Vegetable Curry', (1,2,3): 'Herb Sauté'}
然后通过配方名称[(0,1,2)]访问。但对于整个问题,可以有更好的解决方案。正如评论中所建议的,这些类型的问题属于指导性问题,这不是合适的提问平台欢迎来到StackOverflow,而且。。。在这里申请。StackOverflow是针对特定编程问题的知识库,而不是设计、编码或教程资源。您的问题太广泛,无法解决堆栈溢出问题。也见,;您的大多数代码都应该合并以支持新问题。@Prune设计问题在这里是主题,只要它们足够清楚,并且有现有的“最先进的”答案。事实上,编码是80%的设计,所以如果设计问题在哪里,这里就没有什么可谈的了。
recipe_names = {(0,1,2): 'Vegetable Curry', (1,2,3): 'Herb Sauté'}