Trying to compute sum of list variables over a certain number

I’m on Coursera trying to do this one practice problem and it’s blocking my progress. I’ve analyzed all my past training materials to see if I should know the solution already. no luck!
I don’t really want anyone to spit out an instant answer at me, but I don’t know how to beat this. I’d like to understand how I should recognize the direction to go for the solution so I can grow as a new programmer. I’m including the situation and what code I’ve made already to see where I’m failing.
rainfall_mi is a string that contains the average number of inches of rainfall in
Michigan for every month (in inches) with every month separated by a comma.
Write code to compute the number of months that have more than 3 inches of rainfall.
Store the result in the variable num_rainy_months. In other words, count the number
of items with values > 3.0.

rainfall_mi = "1.65, 1.46, 2.05, 3.03, 3.35, 3.46, 2.83, 3.23, 3.5, 2.52, 2.8, 1.85"
# What I have so far:
num_rainy_months = rainfall_mi.split(',')
acc = 0
for i in num_rainy_months:
    acc = acc + 1
    if i > 3:
        num_rainy_months = num_rainy_months + 1

For future posts, please read the pinned thread to understand how to post code with proper formatting. Indentation is vital in Python, and the forum software will break it if you don’t tell it that the code is code. It also changes quote characters into fancy “smart quote” characters that are not valid in Python code, and sometimes removes or alters other things because they look like HTML.

The problem here isn’t “direction” - your approach to the problem, your algorithm, is completely sound. There are two smaller problems here: a small gap in your knowledge, and careful logical thinking - i.e. attention to detail.

First, the knowledge gap:

The problem here is that after splitting the string, the pieces are still strings. You can’t compare them to an integer like 3 directly - you must do a conversion first:

Onward to logical thinking. You should think carefully about the intended purpose of each variable, and the names that you give them. The result from rainfall_mi.split(‘,’) isn’t the number of rainy months, so it shouldn’t go into num_rainy_months. You should use a different variable and give it a name that explains its purpose to you clearly. Then, of course, to loop over it, you’ll need to use that name in the for loop. I think you’ll also find that everything to do with acc is completely irrelevant, so it should just be removed.

Finally, think about what you do put in num_rainy_months. I see your strategy is to look at each month’s data, and add 1 to num_rainy_months each time you see a rainy month. This is a fine way to do it. But the question is: how should we start off this variable before the loop, in order for that logic to make sense? (Hint: if you don’t see any rainy months the whole time, what should the result be?)

Ok, so here is what I have so far and it seems to work. Can anyone confirm that I won this battle?

rainfall_mi = "1.65, 1.46, 2.05, 3.03, 3.35, 3.46, 2.83, 3.23, 3.5, 2.52, 2.8, 1.85"

rainy_totals = rainfall_mi.split(',')
num_rainy_months = 0
for i in rainy_totals:
    if float(i) > 3.0:
        num_rainy_months = num_rainy_months + 1


here is a potential solution. Note that I have converted the string values into a list of float values so that if the data needs to be analyzed at a later date (I know it is a practice problem for your course but here as a more realistic use case).

rainfall_mi = "1.65, 1.46, 2.05, 3.03, 3.35, 3.46, 2.83, 3.23, 3.5, 2.52, 2.8, 1.85"

rainy_totals = rainfall_mi.split(',')

rain_total_value_list = []
num_rainy_months = 0  # initialize to 0 to start sum

for value in rainy_totals:


    if float(value) > 3.0:

        num_rainy_months += float(value)

1 Like

Pretty much, yep! One small thing. In Python, you don’t “convert” variables; instead, you create new values from existing ones. It’s a subtle difference but an important one. This does nothing:


It figures out what the floating-point value equivalent to that string would be, and then… does nothing with it. However, you have successfully done the same thing on the following line:

if float(i) > 3.0:

where it DOES behave as you’d expect: it calculates the relevant floating-point value, then compares it against 3.

By the way, you can shorthand common operations like “add one to this thing”:

num_rainy_months += 1

The less times you have to write the same name, the less chances you’ll get it wrong :slight_smile:

Going back to how things were when you were struggling, here’s a tip for tackling the problem: If In Doubt, Print It Out! Dump a whole lot of stuff onto the console and see what it looks like. Get to know the reprs of different data types, and you should start seeing important patterns. Notably, had you printed out rainy_totals, you would have seen that it was a list of strings, not numbers, and comparing those against the number 3 won’t work.

This is the one fundamental debugging technique, the one thing you’ll always be able to rely on. It never goes away, no matter what stage you’re at in your programming career, and it’s incredibly useful :slight_smile: