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?)