Exemplo n.º 1
0
def get_cost_per_item(shoplist):
    """A function named get_cost_per_item().
    The key of shoplist should be the item name as found in FRUIT
    The value of shoplist should be an integer indicating
    the number of units to purchase. Return, a new dictionary
    keyed by the name of the fruit with the total cost per-item reflected

    Argument:
    a dictionary called shoplist.

    Return:
    a new dictionary keyed with the total cost per-item reflected.

    Example:"""

    for cfruit, count in shoplist.iteritems():
        for pfruit, price in FRUIT.iteritems():
            if cfruit == pfruit:
                print pfruit, (count * price), count, price
    return
Exemplo n.º 2
0
def get_cost_per_item(shoplist):
    """Creates dict keyed by fruit name.

    Args:
        shoplist (dict): Dictionary of items.

    Returns:
        dict: Keyed by fruit name and total cost/item.

    Examples:
        >>> get_cost_per_item({'Lime': 12, 'Red Pear': 4,
        'Peach': 24, 'Beet': 1})
        {'Lime': 7.08, 'Peach': 95.76, 'Red Pear': 9.96}

        >>> get_cost_per_item({''Red Plum': 3,
        'Bosc Pear': 5, 'Key Lime': 2, 'Beet': 1})

        {'Red Plum': 8.97, 'Key Lime': 7.98, 'Bosc Pear': 9.95}
    """
    return {k: v * z for k, v in shoplist.iteritems()
            for x, z in FRUIT.iteritems() if k == x}