Let’s imagine that you have a list that contains amounts of rainfall for each day, collected by a meteorologist. Her rain gathering equipment occasionally makes a mistake and reports a negative amount for that day. We have to ignore those. We need to write a program to (a) calculate the total rainfall by adding up all the positive integers (and only the positive integers), (b) count the number of positive integers (we will count with “1.0” so that our average can have a decimal point), and (c) print out the average rainfall at the end. Only print the average if there was some rainfall, otherwise print “No rain”.
csp-16-9-1: Construct a program that correctly solves the rainfall problem# initialize the variables rain = [0,5,1,0,-1,6,7,-2,0] sumRain = 0 count = 0 --- # loop through the values in the list for day in rain: --- # if the value of day is positive if day >= 0: --- # add day to the sum # also add one to count sumRain = sumRain + day count = count + 1.0 --- # if count is positive if count > 0: --- # calculate and print the average ave = sumRain / count print("Average",ave) --- # otherwise else: --- # print no rain print("No rain")
Type the program here and try it. Does it work like you thought it would?
Discuss topics in this section with classmates.