A Galway rain counter changes twice. Does it print twice?
A Galway weather station logs four daily rainfall readings. Your program counts how many days had at least two millimetres of rain. The count changes twice as the program runs. How many times does the program actually print?
Start the total at zero. You know what a loop does: it visits each reading in turn. You know what an if statement does: it adds one only if the reading is at least two millimetres. Once you’ve checked every reading, display the total.
Here are four readings in millimetres. The total starts at zero. Watch the index start at zero and move along the list. Each time, the program tests the current reading. Only a true result reaches the add line. The print line sits outside the loop.
At index two, the current reading is one millimetre. The total is already one when this pass begins. Pick the pair that gives the result of the test and the total at the end of the pass.
Start with a total of zero. At index zero, the reading fails the test, so keep zero. At index one, three passes, so add one. At index two, one fails, so keep one. At index three, exactly two passes, so add one to reach two.
Run the same code with readings of three, zero and four millimetres. Start the total at zero again. Pick the three entries you would write in the total column, one after each pass.
Back to the four Galway readings. This is a trace table. Read across each row: the index tells you which reading you used, the condition records true or false, and the last column records the total after the whole pass. Even a false result gets a row.
You might copy zero, one, one and two from the total column into your output answer. Those are values the variable holds as the loop runs. Look at where print sits. It runs once, after the loop, so the only value printed is two.
Put the same counter in a function. Each call starts its own total at zero and returns the count after the loop. The first print tests a list with a reading exactly at the threshold. It prints one. The second tests a list with no qualifying readings. It prints zero.