1

Could someone please help with how the correct way to add math to an already created function that writes output to a text file. The first task was to write the numbers to a output.txt file in values from lowest to highest, no matter the number or how many numbers. This I've done as per my code below. My problem is I now need to show the minimum number on the first line, the maximum number on the second and the average of the numbers on line 3. If anyone could help I would really appreciate the insight

Text file(input.txt)

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

max:18,25,32,14,15,62

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

The output should be:

The min of [1,2,3,4,5,6] is 1

The max of [14,15,18,25,32,62] is 62

The avg of [1,2,3,4,5,6] is 3.4

As mentioned I already have a function to sort the numbers from low to high its just to do the math.

My code so far:

def number1():
    inputFile = open("input.txt", 'r')
    lineList = inputFile.readlines()
    fileHandle = open('inputcopy.txt', 'a')
    for line in lineList:
        numbers = [int(item) for item in line.split(':')[1].split(',')]
        numbers.sort()
        fileHandle.write("%s\n" % numbers)  
number1()

2 Answers2

2

You need to parse the text numbers into ints so python can process them and map the mathematical operations for example like this:

from statistics import mean


with open('input.txt') as file:
    data = {
        line.split(':')[0]: sorted([int(value) for value in line.split(':')[1].split(',')]) for line in file.readlines()
    }

functions = {'min': min, 'max': max, 'avg': mean}

with open('output.txt', 'w') as file:
    file.writelines(
        f"The {function} of {values} is {functions[function](values)}\n" for function, values in data.items()
    )

Which will give you:

>>> The min of [1, 2, 3, 4, 5, 6] is 1
>>> The max of [14, 15, 18, 25, 32, 62] is 62
>>> The avg of [1, 2, 3, 4, 5, 6] is 3.5
marcos
  • 4,473
  • 1
  • 10
  • 24
  • Hi Marcos... For some reason im getting a key error: 'i>>¿min' –  Feb 04 '20 at 14:53
  • @RyCan88 what's the error? I tested it with your sample and generates the correct output, check again your sample file. – marcos Feb 04 '20 at 14:58
  • Thanks Marcos! Did you add it just at the end of my current code? –  Feb 04 '20 at 15:04
  • Hi @RyCan88, no, it does everything, try what I wrote alone. – marcos Feb 04 '20 at 15:06
  • Thanks again Marcos I got it to work! but your code doesn't sort the numbers from low to high then write them as shown –  Feb 04 '20 at 16:08
  • @RyCan88, oh, my bad, i didn't read that part, i updated the answer, it sorts them now. – marcos Feb 04 '20 at 16:12
0

You can use mathematics functions to calculate.

import statistics

def number1():
    inputFile = open("input.txt", 'r')
    lineList = inputFile.readlines()
    fileHandle = open('inputcopy.txt', 'a')
    for line in lineList:
        numbers = [int(item) for item in line.split(':')[1].split(',')]
        numbers.sort()
        if line.split(':')[0] == 'min':
             fileHandle.write("%s\n" % min(numbers))
        elif line.split(':')[0] == 'max':
             fileHandle.write("%s\n" % max(numbers))
        elif line.split(':')[0] == "avg":
             fileHandle.write("%s\n" & statistics.mean(numbers))  
number1()

Take a look at https://www.geeksforgeeks.org/max-min-python/ and https://appdividend.com/2019/01/28/python-statistics-tutorial-mean-function-example/

Nico Zimmer
  • 325
  • 1
  • 12