# Week 4 Ex 7: This program calculates total and monthly average rainfall for selected years # Written by Greg Lamb. # Last modified on 04/21/2015 #User enters number of years worth of data - first variable for loop 1 years = int(raw_input("Enter the number of years worth of data you need: ")) print ("\nI'm looking for {} years worth of data: ".format(years)) #print years for year in range(1, years+1): #loop 1 itera #print(year) #Declare aditional variables for loop 2 rainfallTotal = 0 numMonths = 12 #loop 2 iterates 12 times and keeps a running total of monthly rainfall entered by user for month in range(1,numMonths+1): rainfallInches = float(raw_input("How many inches of rainfall for month number {}: ".format(month))) rainfallTotal = rainfallTotal + rainfallInches print("Month {} had {} inches of rain. Total rainfall in inches so far is {} ".format(month,rainfallInches,rainfallTotal)) #The program design criteria ask for the total number of months for the period as well as #the total number of rainfall inches and monthly average rainfall for the period. totalMonths #and aveRainfall are the remaining variables calculated at the back end of loop 1 totalMonths = years * month #print totalMonths aveRainfall = rainfallTotal / totalMonths #print aveRainfall #Output in next line satisfies the program's design criteria print("Total rainfall for the period is {} inches and the total number of months worth of data is {}, \nwhich results in a monthly average for the period of {} inches.".format(rainfallTotal,totalMonths,aveRainfall))
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question