Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

# Modify the compareCoffeePricesList function and rename # the function to \'exp

ID: 3600727 • Letter: #

Question

# Modify the compareCoffeePricesList function and rename
# the function to 'expensesManager'. As the name suggests,
# the 'cafeNameList' should now be used to store a list of expense
# names and it should be renamed to 'expenseNameList' instead. Unlike,
# the cafeNameList, assume that the user will have multiple cost
# entries for the same name. E.g. your expense name lists might
# contain multiple entries called "junk food" and for each there will
# be a corresponding priceList entry for its cost.
#
# Allow the user to see the total cost of an expense with
# the same name. E.g. if the user enters "junk food", your program should
# add the prices of all the priceList entries for "junk food" and
# show the toal. If they enter "coffee" it should show the total for
# coffee expenses, if any are in the list.

--------------------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------------------

def compareCoffeePricesList():
numCafes=requestInteger("How many cafes?")
coffeePrices=[None]*numCafes

total=0
index=0

while(index<numCafes):
    coffeePrices[index]=requestNumber("Price of a coffee at cafe "+cafeNameList[index])
total=total+coffeePrices[index]
index=index+1

average=total/numCafes

index=0
while(index<numCafes):
if coffeePrices[index]<average:
    print "Cafe:", cafeNameList[index]," coffee is $",(average-coffeePrices[index]),"cheaper than average"
index=index+1

Explanation / Answer

def compareCoffeePricesList():
numCafes=requestInteger("How many cafes?")
coffeePrices=[None]*numCafes

total=0
index=0

while(index<numCafes):
    coffeePrices[index]=requestNumber("Price of a coffee at cafe "+cafeNameList[index])
total=total+coffeePrices[index]
index=index+1

average=total/numCafes

index=0
while(index<numCafes):
if coffeePrices[index]<average:
    print "Cafe:", cafeNameList[index]," coffee is $",(average-coffeePrices[index]),"cheaper than average"
index=index+1