# Finding max value in a series of floats

I am trying to write code that takes a row of floats from a csv file, representing the average temperature in a given month over the course of 70 years, and returns the highest float value of them all. When I print the variable holding the float values, in this case val, I get something similar to:

``````10.56
12.35
11.35
``````

etc.
I know I probably have to write some sort of code that compares the floats to each other and replaces another variable with the higher float, but I can’t seem to figure out how.
Thanks

Please show the code you have so far, so that we can properly understand the actual value in the variable, instead of “something similar” to what you get “when you print” it.

1 Like
``````import csv

def is_float(value):
try:
fval = float(value)
return True
except ValueError:
return False

monthtxt=input("Enter a month: ")
if monthtxt=="January":
monthnum=1
if monthtxt=="February":
monthnum=2
if monthtxt=="March":
monthnum=3
if monthtxt=="April":
monthnum=4
if monthtxt=="May":
monthnum=5
if monthtxt=="June":
monthnum=6
if monthtxt=="July":
monthnum=7
if monthtxt=="August":
monthnum=8
if monthtxt=="September":
monthnum=9
if monthtxt=="October":
monthnum=10
if monthtxt=="November":
monthnum=11
if monthtxt=="December":
monthnum=12

csvfile = open('Tara_Hills_temp.csv')

sumtemps = 0.0
num_temps = 0
val = row[monthnum]
if is_float(val):
sumtemps += float(val)
num_temps += 1

avg = sumtemps/num_temps
print('Average temperature for',monthtxt,'is:',avg)
``````

The code takes a month as an input, and currently prints the average temperature for that month from the data set (csv file). I’m looking for it to print the maximum temperature in that month over the years too.
Link to the data (named ‘Tara_Hills_temp.csv’ in the code)

``````max_temp = 0
max_temp = max(max_temp, float(val))
``````

Python has a builtin function `max(...)`. Don’t hesitate to use it

You managed to do average and you already described how to do max… I don’t see how you’re unable to do it.

1 Like

You could eliminate a dozen `if` statements by using a `dict` or `Enum`:

``````>>> month_num = {
... 'January':1,
... 'February':2,
... 'March':3
... }
>>> month_num['March']
3

>>> from enum import Enum
>>> class Month(Enum):
...  January = 1
...  February = 2
...  March = 3
...
>>> Month.January.value
1
``````

and there’s probably even simpler ways to do it in the `datetime` library.

The code for finding the average was given to me, and the exercise was to add code to find the maximum.
I am a beginner, and thus don’t always see the obvious solution unlike coding gods such as yourself.
Be nice.

The existing code was, presumably, provided as a model that you are expected to understand in order to see some basic tools available to you, so that you can then use the same kinds of tools to solve the problem. So, let’s try to do that analysis. After all, programming is about solving problems, and to learn programming (and it seems very clear that there is someone else - the person or organization that gave you the code - who is trying to teach you programming) we must develop problem-solving skill.

In your own words: outside of programming - with pencil and paper - how is an average calculated? Do you see how this corresponds to the code that was given to you? (Hint: do you see how it looks at each value from the data, one at a time? What does it do with the values as they are encountered? What does it do after it’s finished looking at all the values?

Now that we see how the code is used to implement those steps, we have to work backwards: figure out the new steps what we want to use to solve the new problem, and then figure out what code will implement the new steps.

Step by step, how would you find the maximum, with pencil and paper? Try to describe a step-by-step process. (Hint: it should probably involve looking at each value, one at a time. What would make sense to do with each value, as you encounter it?) Following the model of the existing code for the average, can you envision what the code for the maximum might look like?

Let’s also consider your own initial analysis:

Looking at the existing code, can you see the part where it determines “the floats” that should be compared? Given two float values, do you understand how to compare them, and how to “replace another variable with the higher float”? Where exactly are you getting stuck with this?

My best guess: you are seeing that there is only one new value that you get each time through the loop, but you know that two values are needed to compare to each other. In this case, you just need to think more clearly about what you should be comparing the new value to. (Hint: you already mentioned it.) (Hint: try actually setting up the problem and trying to solve it by hand with pencil and paper. Make sure that you only allow yourself to look at one of the input values at a time. I am confident that you will naturally do the right thing, and only need to notice what you are doing.)

2 Likes

While I understand where you’re coming from, it seems heavy-handed to point out things like this. Clearly there wasn’t any intent here to pass off code as OP’s own - at least, certainly not for the sake of personal gain (in money, academic status or anything else). When I asked to see existing code, it hadn’t occurred to me that OP might not have personally written any yet. The code was helpful because it explained a) that the data doesn’t actually “represent the average temperature in a given month over the course of 70 years” (instead, individual values represent individual temperature data points for each month, and the existing code computes averages); b) that we want a maximum for *the same group of values) that is currently averaged; b) that the desired data comes from a column of the file rather than a row (i.e.: the existing code is iterating over rows, and reading a specific column each time).

The bit about “community” is almost certainly a templated message that comes from the Discourse software, meant to preserve the anonymity of whoever flagged the post.

I don’t think that matters. And the problem here that I see is that this misinformed us about their capabilities/understanding, leading to my earlier confusion (to which I think they were responding) and impacting whether we can appropriately/efficiently help.

Legend! The pen and paper method is actually a stroke of genius! Thinking about it from a different way really seemed to help. Thank you so much for the help!

I’m glad it did help. Many people don’t seem to want to hear it, but programming is what it is. I hope you find it useful in the future, too.