'float'对象不可迭代?

时间:2013-11-09 21:43:11

标签: python typeerror iterable

我正在尝试以英寸为单位汇总总降雨量以显示总数,但是当我运行代码时,我得到一个TypeError,说“'浮动'对象不可迭代”。知道我做错了什么吗?

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 (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

即使向正确的方向推进也会有所帮助。我觉得我正好处理这个问题。

1 个答案:

答案 0 :(得分:2)

get_rain返回

return rain_inches #float

而不是

return rain_in #list