pythonfor-loopexternal-data-source

Please could someone advise on code format


I originally had to write code to write 3 lines to an external text file called output.txt. The first line shows the minimal number of the first line, the second line will show the maximum number of the second line and the third line will show the average of the third line. no matter what numbers are input or length it will still show all the values of min,max and average. The problem is the code is only writing the last average line to the output text file.

My lecturer would like the format to stay the same but he had these comments:

The min and max lines are not being written to the output file. This is because you do not write the values to the report_line variable, which stores the string to write to the output file.

Try initializing report_line to be an empty string before the for loops begin.

You can then use += operator and new line characters to store the output in the report_line variable on each repetition of the for loop.

I tried them but I still get the same result. Only the avg line prints.

outfile = open("output.txt", "w")

with open("input.txt") as f:
    report_line = ""
    for line in f:
        operator, data =  line.lower().strip().split(":")
        line = line.split(":")
        operator = line[0].lower().strip()
        data = line[1].strip().split(",")
        newData = []
    for x in data:
        report_line+='n'
        newData.append(int(x))
        if operator == "min":
            result = min(newData)
        elif operator == "max":
            result = max(newData)
        elif operator == "avg":
            result = sum(newData) / len(newData)
            report_line = "The {} of {} is {}.\n".format(operator, newData, result)

outfile.write(report_line)

outfile.close()

The input:

min:1,2,3,4,5,6
max:1,2,3,4,5,6
avg:1,2,3,4,5,6

The output should be:

The min of [1, 2, 3, 5, 6] is 1.
The max of [1, 2, 3, 5, 6] is 6.
The avg of [1, 2, 3, 5, 6] is 3.4.

Solution

  • You're overwriting your report_line on line 20. Change the = to += instead

    report_line += "The {} of {} is {}.\n".format(operator, newData, result)
                ^
    

    You'd also want to indent the for x in data loop since you're currently only using the last operator, which is avg. You want to do the calculation for every line in your data

    and you might want to remove this line since you're already adding newlines in your report_line statement

    report_line+='n'
    

    The fixed code would be

    outfile = open("output.txt", "w")
    
    with open("input.txt") as f:
        report_line = ""
        for line in f:
            operator, data =  line.lower().strip().split(":")
            line = line.split(":")
            operator = line[0].lower().strip()
            data = line[1].strip().split(",")
            newData = []
            for x in data:
                newData.append(int(x))
                if operator == "min":
                    result = min(newData)
                elif operator == "max":
                    result = max(newData)
                elif operator == "avg":
                    result = sum(newData) / len(newData)
            report_line += "The {} of {} is {}.\n".format(operator, newData, result)
    
    outfile.write(report_line)
    
    outfile.close()