# Average Rainfall # This module calculates the average rainfall over a number of years # Written by Beth Mela # Last updated April 23, 2015 # Define variables and ask user for the number of years of rainfall they want to average totalRainfallInches=0 years=input('Enter the number of years of rainfall to be averaged:') print('The number of years of data to collect is {} years.'.format(years)) # Loop 1 for years for year in range(1,years+1): monthsPerYear=int(12) # Loop 2 for months for month in range(1,monthsPerYear+1): monthlyRainfallInches=float(raw_input('How many inches of rain fell in month {}:'.format(month))) totalRainfallInches=totalRainfallInches + monthlyRainfallInches # Output of total months, total rainfall, and average rainfall per month totalMonths=years*monthsPerYear averageRainfall=float(totalRainfallInches/totalMonths) print('The total rainfall over {} months was {:,.1f} inches and the average rainfall per month was {:,.1f} inch(es).'.format(totalMonths, totalRainfallInches, averageRainfall)) # End module
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