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

# Modify EITHER the \'compareCoffeePricesList\' function OR the \'cafeDiskDemo\'

ID: 3594411 • Letter: #

Question

# Modify EITHER the 'compareCoffeePricesList' function OR the 'cafeDiskDemo' function
# so that instead of having the cafes numbered 0, 1, 2, etc., they are named.
# When taking inputs, the function(s) must take in a cafe name and a
# price of coffee at that cafe. Store the names in a separate list
# named cafeNameList. Both lists will have the same number of items
# (i.e. you do not need to consider multiple coffee prices per cafe).

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

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

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 "+str(index))
total=total+coffeePrices[index]
index=index+1

average=total/numCafes

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

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

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

def cafeDiskDemo():
useAFile=requestInteger("Load cafe data from file? [1:Yes]")
coffeePrices=[]
if useAFile==1:
    filePath=pickAFile()
    file=open(filePath,"r")
    coffeePrices=file.readlines()
    file.close()
  
numCafes=len(coffeePrices)
total=0
index=0
while(index<numCafes):
    printNow("Price of coffee at cafe"+str(index)+" is $"+coffeePrices[index])
    coffeePrices[index]=float(coffeePrices[index])
    total=total+coffeePrices[index]
    index=index+1

newPriceEntry=requestNumber("Price of coffee at cafe "+str(numCafes)+" (negative to stop):")
while(newPriceEntry>=0):
    coffeePrices.append(newPriceEntry)
    total=total+newPriceEntry
    numCafes=numCafes+1
    newPriceEntry=requestNumber("Price of coffee at cafe "+str(numCafes)+" (negative to stop):")
  
average=total/numCafes

index=0
while(index<numCafes):
    if coffeePrices[index]<average:
      printNow("Cafe "+str(index)+" has coffee that's $"+str(average-coffeePrices[index])+" cheaper than average")
    index=index+1

useAFile=requestInteger("Save cafe data to file? [1:Yes]")
if useAFile==1:
    filePath=pickAFile()
    filePath=pickAFolder()+requestString("Enter file name to overwrite (e.g. data.txt)")
    file=open(filePath,"w")
    index=0
    while(index<numCafes):
      file.write(str(coffeePrices[index])+" ") index=index+1
    file.close()

Explanation / Answer

Let 'cafe_names' be the file name having the names of all the cafes.

Following code performs the required corrections to the given code to display cafe names.


# Modify EITHER the 'compareCoffeePricesList' function OR the 'cafeDiskDemo' function
# so that instead of having the cafes numbered 0, 1, 2, etc., they are named.
# When taking inputs, the function(s) must take in a cafe name and a
# price of coffee at that cafe. Store the names in a separate list
# named cafeNameList. Both lists will have the same number of items
# (i.e. you do not need to consider multiple coffee prices per cafe).
#--------------------------------------------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------------------------------------------
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
#--------------------------------------------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------------------------------------------
def cafeDiskDemo():
useAFile=requestInteger("Load cafe data from file? [1:Yes]")
coffeePrices=[]
if useAFile==1:
filePath=pickAFile()
file=open(filePath,"r")
coffeePrices=file.readlines()
cafeNameList = [line.rstrip(' ') for line in open('cafe_names')]
file.close()
  
numCafes=len(coffeePrices)
total=0
index=0
while(index<numCafes):
printNow("Price of coffee at cafe"+cafeNameList[index]+" is $"+coffeePrices[index])
coffeePrices[index]=float(coffeePrices[index])
total=total+coffeePrices[index]
index=index+1
newPriceEntry=requestNumber("Price of coffee at cafe "+str(numCafes)+" (negative to stop):")
while(newPriceEntry>=0):
coffeePrices.append(newPriceEntry)
total=total+newPriceEntry
numCafes=numCafes+1
newPriceEntry=requestNumber("Price of coffee at cafe "+str(numCafes)+" (negative to stop):")
  
average=total/numCafes
index=0
while(index<numCafes):
if coffeePrices[index]<average:
printNow("Cafe "+cafeNameList[index]+" has coffee that's $"+str(average-coffeePrices[index])+" cheaper than average")
index=index+1
useAFile=requestInteger("Save cafe data to file? [1:Yes]")
if useAFile==1:
filePath=pickAFile()
filePath=pickAFolder()+requestString("Enter file name to overwrite (e.g. data.txt)")
file=open(filePath,"w")
index=0
while(index<numCafes):
file.write(str(coffeePrices[index])+" ") index=index+1
file.close()