Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def main():
- #initialize variables so incomplete code won't cause errors
- #can be deleted once code is finished
- totalRainfall = 0
- minRainfall = 0
- maxRainfall = 0
- #prints the progam title and a blank line
- print("Rainfall Calculator with Array")
- print()
- #declares an empty array (list) called rainfall to
- #which you can append values with the append method
- rainfall=[]
- #for loop to loop from 0 to 11
- #remember the 1st element of an array is at subscript 0
- for index in range(12):
- #input rainfall for month and store as float in variable
- rainInput = float(input("Inches of rainfall for month #"+str(index+1)+": "))
- #STEP 1 - append the value in the variable to the array
- rainfall.append(rainInput)
- #STEP 2 - call the calcTotal() function and pass it the array
- #store the returned value in totalRainfall
- totalRainfall = calcTotal(rainfall)
- #calculate the average (total / 12) and store it in a variable
- avgRainfall = totalRainfall / 12
- #STEP 4 - call the findMin() function and pass it the array
- #store the returned value in minRainfall
- minRainfall = findMin(rainfall)
- #STEP 6 - call the findMax() function and pass it the array
- #store the returned value in maxRainfall
- maxRainfall = findMax(rainfall)
- #print the total rainfall
- print("Total rainfall in inches:", totalRainfall)
- #print the average rainfall
- print("Average rainfall in inches:", avgRainfall)
- #print the month with the minimum rainfall
- print("Lowest rainfall in inches:", minRainfall)
- #print the month with the maximum rainfall
- print("Highest rainfall in inches:", maxRainfall)
- #calcTotal loops thru the array and uses a running total
- #to find the total rainfall
- def calcTotal(rainfall):
- total = 0
- #STEP 3 - insert your for loop code below
- for r in rainfall:
- total = total + r
- #insert your code for Step 3 above
- return total
- #findMin loops thru the array and finds the lowest rainfall
- def findMin(array):
- minRain = array[0]
- #STEP 5 - insert your for loop code below
- for rain in array:
- if rain < minRain:
- minRain = rain
- #insert your for Step 5 above
- return minRain
- #findMax loops thru the array and finds the highest rainfall
- def findMax(array):
- maxRain = array[0]
- #STEP 7 - insert your for loop code below
- for rain in array:
- if rain > maxRain:
- maxRain = rain
- #insert your for Step 7 above
- return maxRain
- #call main() to start program
- main()
- Prompt Part 1: http://i.gyazo.com/9d167bec861f8915544efcb8f5a14d61.png
- Prompt Part 2: http://i.gyazo.com/2c7463d0417e8fbdb2a4159873f52638.png
- Prompt Part 3: http://i.gyazo.com/d64ddb31a2d65ced015b10da555e1eb9.png
- What happens when I run the program:
- Rainfall Calculator with Array
- Inches of rainfall for month #1: 9.82
- Inches of rainfall for month #2: 3.65
- Inches of rainfall for month #3: 6.29
- Inches of rainfall for month #4: 5.39
- Inches of rainfall for month #5: 7.70
- Inches of rainfall for month #6: 3.89
- Inches of rainfall for month #7: 8.64
- Inches of rainfall for month #8: 5.15
- Inches of rainfall for month #9: 4.00
- Inches of rainfall for month #10: 0.67
- Inches of rainfall for month #11: 3.75
- Inches of rainfall for month #12: 8.87
- Total rainfall in inches: 67.82000000000001
- Average rainfall in inches: 5.651666666666667
- Lowest rainfall in inches: 0.67
- Highest rainfall in inches: 9.82
- The only issue I'm having now, is that the Total rainfall in inches is supposed to be 67.82 and Average rainfall in inches is supposed to be 5.65 and trying to round it or format it doesn't work because that just causes a syntax error.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement