Question #318195

Problem 1: Add a buyLotsOfFruit(orderList) function to buyLotsOfFruit.py which takes a list of (fruit,pound) tuples and returns the cost of your list. If there is some fruit in the list which doesn't appear in fruitPrices it should print an error message and return None (which is like nil in Scheme). Please do not change the fruitPrices variable.

Test Case: We will check your code by testing that the script correctly outputs Cost of [('apples', 2.0), ('pears', 3.0), ('limes', 4.0)] is 12.25


Expert's answer

FruitPrices = {'apples':2.00, 'pears': 3.0, 'lime': 4.00}

AllPrice = 0.0 

def buyFruit(orderList):
    AllPrice = 0.0 
    for cost,price in FruitPrices.items():
        if cost not in FruitPrices:
            return None
        else:           
           AllPrice = AllPrice+FruitPrices[cost] 
    return AllPrice
print(buyFruit(0))
LATEST TUTORIALS
APPROVED BY CLIENTS