Answer to Question #318195 in Python for hatipo

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


1
Expert's answer
2022-03-25T15:12:44-0400
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))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS