Python遍历元组列表

问题描述

我具有以下功能:

def buyLotsOfFruit(orderlist):
    totalCost = 0.0
    for fruit in orderlist:
        if fruit not in fruitPrices:
            return None
        else:
            totalCost = totalCost+fruitPrices.get(fruit)*pound
            return totalCost

其中:

fruitPrices = {'apples': 2.00,'oranges': 1.50,'pears': 1.75,'limes': 0.75,'strawberries': 1.00}

假设我有以下订单清单:

orderlist = [('apples',2),('pears',3),('limes',4)]

当我希望它在fruitPrices列表中查看并检查是否所有项目都存在时,循环始终不返回任何内容,它将计算总价格。列出的项目,否则如果缺失则不返回

注意:磅是元组列表中与订单列表中每个水果相关联的整数。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)