(USE PYTHON) Fill in the code \"\"\" To run this script, type python buyLotsOfFr
ID: 3686225 • Letter: #
Question
(USE PYTHON) Fill in the code
"""
To run this script, type
python buyLotsOfFruit.py
Once you have correctly implemented the buyLotsOfFruit function,
the script should produce the output:
Cost of [('apples', 2.0), ('pears', 3.0), ('limes', 4.0)] is 12.25
"""
fruitPrices = {'apples':2.00, 'oranges': 1.50, 'pears': 1.75,
'limes':0.75, 'strawberries':1.00}
def buyLotsOfFruit(orderList):
"""
orderList: List of (fruit, numPounds) tuples
Returns cost of order
"""
totalCost = 0.0
"*** YOUR CODE HERE ***"
for fruit, fruitprice in orderList:
if fruit not in fruitprices:
print "no fruit"
else:
totalcost = sum([num*fruitPrices[fruit] for fruit, num in orderList])
return totalCost
# Main Method
if __name__ == '__main__':
"This code runs when you invoke the script from the command line"
orderList = [ ('apples', 2.0), ('pears', 3.0), ('limes', 4.0) ]
print 'Cost of', orderList, 'is', buyLotsOfFruit(orderList)
Explanation / Answer
fruitPrices = {'apples':2.00, 'oranges': 1.50, 'pears': 1.75,
'limes':0.75, 'strawberries':1.00}
totalCost=0.0
def buyLotsOfFruit(orderList):
for fruit in orderList:
if fruit[0] in fruitPrices:
totalCost = totalCost +(fruit[1]*fruitPrices[fruit[0]])
#print fruit[0]
return totalCost
# Main Method
if __name__ == '__main__':
"This code runs when you invoke the script from the command line"
orderList = [ ('apples', 2.0), ('pears', 3.0), ('limes', 4.0) ]
print 'Cost of', orderList, 'is', buyLotsOfFruit(orderList)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.