python - Why is my list index out of range? -


def main():     print("you haved activated weather pro 3.0")     rain_inputs()     rain_calc()  def rain_inputs():     global rain     rain = []     x in range(1, 13):         try:             rain_meter = float(input("what rainfall? "))             rain.append(rain_meter)             if x == 4:                 print("8 more months go!")             elif x == 7:                 print("5 more months go!")             elif x == 9:                 print("2 more go! smash keys!")             elif x == 13:                 return         except exception err:             err = ("you should putting in numbers!")             print(err) 

rain_calc supposed calculate total of listed inputs.

def rain_calc():     rain_math =(rain[0] + rain[1] + rain[2] + rain[3])     rain_math2 =(rain[5] + rain[6] + rain[7] + rain[8])     rain_math3 =(rain[9] + rain[10] + rain[11] + rain[12])     rain_total =(rain_math + rain_math2 + rain_math3)     print(rain_total)     main()    

the error occurs in rain-math3:

programming/rainfall.py", line 30,           in rain_calc rain_math3 =(rain[9] + rain[10] + rain[11] + rain[12]) indexerror: list index out of range 

rain[12] out of range because rain has 12 elements in it. missed rain[4], wanted this:

def rain_calc():     rain_math =(rain[0] + rain[1] + rain[2] + rain[3])     rain_math2 =(rain[4] + rain[5] + rain[6] + rain[7])     rain_math3 =(rain[8] + rain[9] + rain[10] + rain[11])     rain_total =(rain_math + rain_math2 + rain_math3)     print(rain_total) 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -