# Rewrite either the \'compareCoffeePricesList\' function OR the \'cafeDiskDemo\
ID: 3594221 • Letter: #
Question
# Rewrite 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
I have rewritten compareCoffeePricesList to take cafe names also as input
I have used requestString method, if you do not have this method then just replace it with input (python 3) or raw_input (python 2)
def compareCoffeePricesList():
numCafes=requestInteger("How many cafes?")
coffeePrices=[None]*numCafes
cafeNameList = [None]*numCafes
total=0
index=0
while(index<numCafes):
cafeNameList[index]=requestString("Name of the cafe "+str(index))
coffeePrices[index]=requestNumber("Price of a coffee at cafe "+str(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()
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()
# copyable codelink: https://paste.ee/p/e9uTH
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.