Analysing precipitation data.
Select the button below to open the Python program in a new window. Run the program and read the lines of code to see if you can understand how it works. It will be helpful to arrange your display so that you can have this browser window on one side of the screen and the code on the other.
Watch this video to learn about the new concepts shown in the program:
for x in y
Iterates over all the items in list y, with x being assigned to the data of each element one at a time in each iteration.
E.g. if `y = ["Hello", "World"]` then x would be `"Hello"` in the first iteration and `"World"` in the second iteration.
for x in range(len(y))
Iterates over all the items in list y, with x being assigned the index of each element in the list in each iteration.
E.g. if `y = ["Hello", "World"]` then x would be 0 in the first iteration and 1 in the second iteration.
Questions to think about with this program to check your understanding:
What is the purpose of `value` in the function `count1`?
`value` holds one item of data from the list in each iteration.
Explain why a for loop has been used in the procedure `count2` instead of the while loop shown below:
def count3(item, data):
count = 0
index = 0
while item != data[index]:
if item == data[index]:
count = count + 1
index = index + 1
return count
The while loop will stop as soon as the first data item is found. A for loop will iterate through all the items.
`while` is useful if you want to find the first instance of an item in a list and then stop. `for` is useful if you need to consider all the items in a list.
Change the program so that:
Both `analyse1` and `analyse2` functions:
You should apply your changes to both functions to illustrate the two different approaches.
Days with no rain: 6
Average rainfall: 0.12
Highest rainfall: 0.4
Use the flowcharts if you need more support in understanding the steps required to meet the criteria. Use the Parsons code sorting exercise only if you are really stuck.
Run your code to check that your program has met the success criteria and produces the required output.
Days with no rain: 6
Average rainfall: 0.12
Highest rainfall: 0.4