使用字典计算购物清单 [英] Computing shopping-list total using dictionaries

查看:164
本文介绍了使用字典计算购物清单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试调用列表中列出的购物清单的总字典值,但错误已经到来,总和为10.5而不是7.5,它应该列出列表中的项目的总价格,任何列表。

I tried calling the total of dictionary values for a shopping list made in list, but error has been coming up and summing to 10.5 instead of 7.5, it is supposed to give out the total price of items in a list, any list.

stock = {
    "banana": 6,
    "apple": 0,
    "orange": 32,
    "pear": 15
}

prices = {
    "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3
}

# Write your code below!

def compute_bill(food):
    total = 0
    for item in food:
        item = shopping_list(prices[key])
        total += item
    return total
shopping_list = ["banana", "orange", "apple"]  


推荐答案

您可以使用列表推导...

You can use list comprehensions ...

sum ] for s in shopping_list])

这篇关于使用字典计算购物清单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆