-3

I have a text file with many lines of numbers and wish to find the highest number. I have already found the average of all the numbers.

Here is my code so far:

file = open('max_vind_sola_enkelttall.txt', 'r')
lines = file.read

sum = 0
for lines in file:
    sum += float(lines)

amount = 362
average = sum/amount 
print("average is: ", average)

file.close()
petezurich
  • 9,280
  • 9
  • 43
  • 57
Jonas AAT
  • 11
  • 1
  • 1
    The solution is egregiously easy. Just go over every line and check if it is higher than the highest number so far. If it is, set it as the highest number. When you're done, return the highest number. – Michael M. Sep 18 '22 at 18:45
  • 1
    It's generally a bad idea to use `sum` as a variable name, since the word `sum` is already the name of a built in function – Ben Grossmann Sep 18 '22 at 18:49
  • As a nice little one line solution, `max(map(float,file.read().split()))` will give you the maximum that you're looking for. Similarly, `sum(map(float,file.read().split()))` gives you the sum – Ben Grossmann Sep 18 '22 at 18:56

3 Answers3

1

Dealing with min and max are very similar to dealing with accumulating the sum/total.

with open('max_vind_sola_enkelttall.txt') as file:

    total = 0
    count = 0
    mn = float('inf')
    mx = float('-inf')

    for line in file:
        count += 1
        v = float(line)
        total += v
        if v < mn:
            mn = v
        if v > mx:
            mx = v

average = total / count
print("average is: ", average)
print("max is: ", mx)
print("min is: ", mn)

Note that I used a with clause to make sure that the file gets closed properly no matter what else happens. These days, we should seldom be calling close() on a stream explicitly, especially when reading and writing local files like this.

CryptoFool
  • 21,719
  • 5
  • 26
  • 44
  • 2 cents from the peanut gallery: generally speaking, it's poor practice to use `min` and `max` as variable names; I personally go for `mn` and `mx`. Also, a handy option for an initial minimum is `float('inf')`. – Ben Grossmann Sep 18 '22 at 19:00
  • 1
    @BenGrossmann - yeah yeah...totally agree. Just laziness in a situation where it really doesn't matter. and DOH! I thought for just a few seconds about how infinity was done. Of course. So simple. Gonna try hard to not forget that again. You can't just put a '-' in front of that whoe expression. It has to be `float('-inf')` if I recall correctly. – CryptoFool Sep 18 '22 at 19:31
  • 1
    @BenGrossmann - all of your suggestions applied – CryptoFool Sep 18 '22 at 19:35
  • Thanks for humoring me, hope you didn't mind the pedantic comment. My computer doesn't seem to have an issue with `-float("inf")`, but that might be a version thing. Also noteworthy perhaps that if you happen to be importing from the math module anyway, `math.inf` is an option (and similarly `numpy.inf`). – Ben Grossmann Sep 18 '22 at 19:41
  • @BenGrossmann - thanks for all of the Python infinity trivia, lol. It just didn't cross my mind to use infinity at all. My mind was thinking about a MAX_XXX of some sort, which maybe makes no sense, especially for floats. I know that there's no max in in Python because there are no limits on the size of an int. I honestly just immediately chose to punt on all of that...quite obviously. If it had been real code, I would have spent the 30 to 60 seconds necessary to find an acceptable solution. – CryptoFool Sep 19 '22 at 02:13
  • No judgment from me, I definitely agree that this context didn't demand a perfectly thought out solution. I just thought it was worth commenting on – Ben Grossmann Sep 19 '22 at 02:57
0
file = open('max_vind_sola_enkelttall.txt', 'r')
lines = file.read

sum = 0
max = 0
for lines in file:
    sum += float(lines)
    if float(lines) > max:
        max = lines
amount = 362
average = sum / amount
print("average is: ", average)
print("max is: ", max)
Brady R
  • 190
  • 1
  • 3
  • There's no need to answer questions like this. Not only do they show zero effort, but they have also been answered thousands of times and are clearly duplicates. – Michael M. Sep 18 '22 at 18:50
  • Also, writing your code as an html snippet suppresses the python syntax-highlighting that SO does (because of the Python tag, I believe) – Ben Grossmann Sep 18 '22 at 18:52
-1

If your numbers are a list of floats, you can use the max function with your list as the input.

ex.

file = open('max_vind_sola_enkelttall.txt', 'r')
lines = file.readlines()

lines = [float(x) for x in lines]

largest_float = max(lines)
print(largest_float)

https://docs.python.org/3/library/functions.html#max

LarryDCJ
  • 3
  • 2