0

I'm trying to aggregate total rainfall in inches to display the total but when I run the code I get a TypeError saying that "'float' object is not iterable". Any idea what I've done wrong?

months = 12

def main():

    rain = get_rain()

    total = get_total(rain)

    avg = total / len(rain)

    low = min(rain)

    high = max(rain)

    print('The total rainfall in inches for the year is: ', format(total, ',.2f'))
    print()
    print('The average monthly rainfall this year was: ', format(avg, ',.2f'))
    print()
    print('The lowest rainfall in inches this year was: ', format(low, ',.2f'))
    print()
    print('The highest rainfall in inches this year was: ', format(high, ',.2f'))
    print()

def get_rain():

    rain_in = []

    print('Enter the amount of rainfall in inches for each month of the year.')
    print('------------------------------------------------------')

    for index in range(months):
          print('Enter the total rainfall in inches for month #', index + 1, ': ', sep='', end='')
          rain_inches = float(input())

          rain_in.append(rain_inches)

    return rain_inches

def get_total(rain_in_list):

          total = 0.0

          for num in rain_in_list:
              total += num

          return total

main()

Traceback:

Traceback (most recent call last):
  File "C:/Users/Robert Rodriguez/Desktop/fall 2013-2014/Intro-python/python6/exercise8-3.py", line 50, in <module>
    main()
  File "C:/Users/Robert Rodriguez/Desktop/fall 2013-2014/Intro-python/python6/exercise8-3.py", line 9, in main
    total = get_total(rain)
  File "C:/Users/Robert Rodriguez/Desktop/fall 2013-2014/Intro-python/python6/exercise8-3.py", line 45, in get_total
    for num in rain_in_list:
TypeError: 'float' object is not iterable

Even a bit of a push in the right direction would help. I feel like I'm right on the edge of figuring this out.

4

1 回答 1

2

get_rain你回来

return rain_inches #float

代替

return rain_in #list
于 2013-11-09T21:45:54.887 回答