I am a beginner in Python and am struggling on retrieving an element out of a tuple from a list. What I am trying to do is to get the value of a fruit and multiply it by the quantity needed. An example below will show you what I mean. I can not figure out how to get the second element in the tuple.
##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
for fruit,price in fruitPrices.items():
if fruit not in fruitPrices:
print 'not here!'
else:
totalCost = totalCost +fruitPrices[fruitPrices.index(fruit)].key() * price
return totalCost
It is mostly in my else statement that I can not get it working. All help is greatly appreciated!