gpt-3.5-turbo-0613· openai
Suzhen/CodeChat
correct the below code to print the output in salesreport.txt salesData =…
- User
correct the below code to print the output in salesreport.txt
salesData = readData("icecream sales.txt") printReport(salesData)def readData(fileName) : # Create an empty dictionary. salesData = {}
infile = open(filename, "r") # Read each record from the file. for line in infile: fields = line.split(":") flavor = fields[0] salesData[flavor] = buildList(fields) infile.close() return salesDatadef buildList(fields) : storeSales = [] for i in range(1, len(fields)) : sales = float(fields[i]) storeSales.append(sales)
return storeSalesdef printReport(salesData) : # Find the number of stores as the length of the longest store sales list. numStores = 0 for storeSales in salesData.values() : if len(storeSales) > numStores : numStores = len(storeSales)
# Create a list od store totals. storeTotals = [0.0] # Print the flavor sales. for flavor in sorted(storeSales) : print("%-15s" % flavor) flavorTotal = 0.0 storeSales = salesData[flavor] for i in range(len(storeSales)) : sales = storeSales[i] flavorTotal = flavorTotal + sales storeTotals[i] = storeTotals[i] + sales print("%10.2f" %sales, end = "") print("%15s" %"", end = "") for i in range(numStores) : print("%10.2f" %storeTotals[i], end = "") print() - Assistant
To correct the code to print the output in salesreport.txt, the following changes need to be made:
- Add a file parameter to the printReport() function to specify the file where the output should be written.
- Open the salesreport.txt file in write mode before calling the printReport() function.
- Modify the print statements in the printReport() function to write to the file instead of printing to the console.
Here is the corrected code:
salesData = readData("icecream sales.txt") with open("salesreport.txt", "w") as outfile: printReport(salesData, outfile) def readData(fileName): # Create an empty dictionary. salesData = {} infile = open(fileName, "r") # Read each record from the file. for line in infile: fields = line.split(":") flavor = fields[0] salesData[flavor] = buildList(fields) infile.close() return salesData def buildList(fields): storeSales = [] for i in range(1, len(fields)): sales = float(fields[i]) storeSales.append(sales) return storeSales def printReport(salesData, outfile): # Find the number of stores as the length of the longest store sales list. numStores = 0 for storeSales in salesData.values(): if len(storeSales) > numStores: numStores = len(storeSales) # Create a list of store totals. storeTotals = [0.0] * numStores # Write the flavor sales to the file. for flavor in sorted(salesData.keys()): outfile.write("%-15s" % flavor) flavorTotal = 0.0 storeSales = salesData[flavor] for i in range(len(storeSales)): sales = storeSales[i] flavorTotal += sales storeTotals[i] += sales outfile.write("%10.2f" % sales) outfile.write("%15s" % "") for i in range(numStores): outfile.write("%10.2f" % storeTotals[i]) outfile.write("\n")This code will read data from "icecream sales.txt", create a sales report, and write the output to "salesreport.txt".