Analysing precipitation data.
Watch this video to learn about the new concepts shown in the program:
for x in yIterates 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 analyse1?
value holds one item of data from the list in each iteration.
Explain why a for loop has been used in the function analyse2 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
# -------------------------
# Main program
# -------------------------
---
daily_rainfall_mm = [0.1, 0.0, 0.2, 0.4, 0.1, 0.0, 0.0,
0.0, 0.3, 0.3, 0.2, 0.0, 0.0, 0.1]
---
analysis = analyse1(daily_rainfall_mm)
---
print("Days with no rain:", analysis[0])
---
print("Average rainfall: {0:.2f}".format(analysis[1]))
---
print("Highest rainfall:", analysis[2])
analyse1
# Function to analyse a rainfall data set
def analyse1(data):
---
# Initialise variables
count = 0 # Count of days with no rainfall
average = 0 # The average rainfall recorded in the period
highest = 0 # The highest rainfall recorded in the period
---
# Consider each item of data in the data set
for value in data:
---
# If the value is zero then there was no rainfall
if value == 0:
---
count = count + 1
---
# Keep a running total for the average
average = average + value
---
# If the value is greater than the highest so far, record this as the highest instead
if value > highest:
---
highest = value
---
# Calculate the average
---
average = average / len(data)
---
return [count, average, highest]
analyse1
# Function to analyse a rainfall data set
def analyse2(data):
---
# Initialise variables
count = 0 # Count of days with no rainfall
average = 0 # The average rainfall recorded in the period
highest = 0 # The highest rainfall recorded in the period
---
# Consider each item of data in the data set
for index in range(len(data)):
---
# If the value is zero then there was no rainfall
if data[index] == 0:
---
count = count + 1
---
# Keep a running total for the average
average = average + data[index]
---
# If the value is greater than the highest so far, record this as the highest instead
if data[index] > highest:
---
highest = data[index]
---
# Calculate the average
average = average / len(data)
---
return [count, average, highest]