# ------------------------------------------------------------ #Student: Austin Wells #Class: CIS122 #Date: 4/23/2015 #Program Title: Chapter 5 Programming Excercise 7.py #(Average Rainfall) #Program Description: This program will collect user input to calculate and display the average rainfall over a period of years. # ------------------------------------------------------------ monthList = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] # Prints the distance traveled def printMonthRainfallAvg(averageRainfall, numOfYears, totalRainfall): if(numOfYears == 1): plural = "" else: plural = "s" print'\n Number of Months Total Inches of Rainfall Average Rainfall per Month for ' + str(numOfYears) + ' year{}' .format(plural) print'---------------------------------------------------------------------------------------------------------------------------------' print ' {} {} {:0.2f}' .format((numOfYears * 12),totalRainfall, averageRainfall) # total rainfall input function def totalRainfall(numOfYears): totalRainfall = 0 if(numOfYears > 1): for i in range(1,numOfYears + 1): for month in monthList: totalRainfall += promptForNumber("How many inches of rain fell in " + month + " of year " + str(i)) else: for month in monthList: totalRainfall += promptForNumber("How many inches of rain fell in " + month) return totalRainfall # prompts user for input. def promptForNumber(strPrompt): userInput = int(raw_input(strPrompt + ": ")) print '{}' .format(userInput) return userInput # inputs the item list and calculates their total and returns the total def averageRainfall(totalRainfall,numOfYears): averageRainfall = totalRainfall / float((numOfYears * 12)) return averageRainfall #main driver function def main(): try: numOfYears = promptForNumber("How many years would you like to calculate the average rainfall for?") totalRainfall1 = totalRainfall(numOfYears) printMonthRainfallAvg(averageRainfall(totalRainfall1,numOfYears), numOfYears, totalRainfall1) except (ValueError): print '\n\nInvalid input. You have entered a non integer. Please enter an integer' except (EOFError): print '\n\nInvalid input. You have forgotten to add input. Please Reset program and Please enter an integer' main()
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