Advertisement
CorporalCompassion

Not as confused as shit ^_^

Apr 11th, 2015
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.78 KB | None | 0 0
  1. def main():
  2.     #initialize variables so incomplete code won't cause errors
  3.     #can be deleted once code is finished
  4.  
  5.  
  6.     totalRainfall = 0
  7.     minRainfall = 0
  8.     maxRainfall = 0
  9.  
  10.     #prints the progam title and a blank line
  11.     print("Rainfall Calculator with Array")
  12.     print()
  13.  
  14.     #declares an empty array (list) called rainfall to
  15.     #which you can append values with the append method
  16.     rainfall=[]
  17.  
  18.     #for loop to loop from 0 to 11
  19.     #remember the 1st element of an array is at subscript 0
  20.     for index in range(12):
  21.  
  22.         #input rainfall for month and store as float in variable
  23.         rainInput = float(input("Inches of rainfall for month #"+str(index+1)+": "))
  24.  
  25.         #STEP 1 - append the value in the variable to the array
  26.         rainfall.append(rainInput)
  27.  
  28.     #STEP 2 - call the calcTotal() function and pass it the array
  29.     #store the returned value in totalRainfall
  30.  
  31.     totalRainfall = calcTotal(rainfall)
  32.  
  33.     #calculate the average (total / 12) and store it in a variable
  34.     avgRainfall = totalRainfall / 12
  35.  
  36.     #STEP 4 - call the findMin() function and pass it the array
  37.     #store the returned value in minRainfall
  38.    
  39.     minRainfall = findMin(rainfall)
  40.    
  41.     #STEP 6 - call the findMax() function and pass it the array
  42.     #store the returned value in maxRainfall
  43.    
  44.     maxRainfall = findMax(rainfall)
  45.    
  46.     #print the total rainfall
  47.     print("Total rainfall in inches:", totalRainfall)
  48.  
  49.     #print the average rainfall
  50.     print("Average rainfall in inches:", avgRainfall)
  51.  
  52.     #print the month with the minimum rainfall
  53.     print("Lowest rainfall in inches:", minRainfall)
  54.  
  55.     #print the month with the maximum rainfall
  56.     print("Highest rainfall in inches:", maxRainfall)
  57.  
  58. #calcTotal loops thru the array and uses a running total
  59. #to find the total rainfall
  60. def calcTotal(rainfall):
  61.     total = 0
  62.     #STEP 3 - insert your for loop code below
  63.     for r in rainfall:
  64.         total = total + r
  65.  
  66.  
  67.     #insert your code for Step 3 above
  68.     return total
  69.  
  70. #findMin loops thru the array and finds the lowest rainfall
  71. def findMin(array):
  72.     minRain = array[0]
  73.     #STEP 5 - insert your for loop code below
  74.     for rain in array:
  75.         if rain < minRain:
  76.             minRain = rain
  77.     #insert your for Step 5 above
  78.     return minRain
  79.  
  80. #findMax loops thru the array and finds the highest rainfall
  81. def findMax(array):
  82.     maxRain = array[0]
  83.     #STEP 7 - insert your for loop code below
  84.     for rain in array:
  85.         if rain > maxRain:
  86.             maxRain = rain
  87.    
  88.  
  89.     #insert your for Step 7 above
  90.     return maxRain
  91.  
  92. #call main() to start program
  93. main()
  94.  
  95.  
  96. Prompt Part 1: http://i.gyazo.com/9d167bec861f8915544efcb8f5a14d61.png
  97. Prompt Part 2: http://i.gyazo.com/2c7463d0417e8fbdb2a4159873f52638.png
  98. Prompt Part 3: http://i.gyazo.com/d64ddb31a2d65ced015b10da555e1eb9.png
  99.  
  100. What happens when I run the program:
  101.  
  102. Rainfall Calculator with Array
  103.  
  104. Inches of rainfall for month #1: 9.82
  105. Inches of rainfall for month #2: 3.65
  106. Inches of rainfall for month #3: 6.29
  107. Inches of rainfall for month #4: 5.39
  108. Inches of rainfall for month #5: 7.70
  109. Inches of rainfall for month #6: 3.89
  110. Inches of rainfall for month #7: 8.64
  111. Inches of rainfall for month #8: 5.15
  112. Inches of rainfall for month #9: 4.00
  113. Inches of rainfall for month #10: 0.67
  114. Inches of rainfall for month #11: 3.75
  115. Inches of rainfall for month #12: 8.87
  116. Total rainfall in inches: 67.82000000000001
  117. Average rainfall in inches: 5.651666666666667
  118. Lowest rainfall in inches: 0.67
  119. Highest rainfall in inches: 9.82
  120.  
  121. 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