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.

5
  • 1
    Please post the full traceback Commented Nov 9, 2013 at 21:44
  • input() accepts a prompt string, so it's better to use input("Enter the amount of rainfall..."). Commented Nov 9, 2013 at 21:49
  • Unrelated to your exception: You may be able to simplify some of your print calls by using the str.format method rather than the built-in format function. For instance, print('The total rainfall in inches for the year is: {.2f}'.format(total)). For your input prompt, you can do this formatting right in the input call (it will be printed at the start of the line where the input is requested): input('Enter the total rainfall in inches for month #{}: '.format(index + 1)) Commented Nov 9, 2013 at 21:50
  • Just a note: Your get_toal function already exists in Python. It is called sum. Commented Nov 9, 2013 at 22:00
  • Thanks for the tips guys! @blckknght- this is some helpful advice, I'll play with this and see what I can learn :) ryanthompson- makes sense, how would I implement this? Commented Nov 10, 2013 at 1:22

1 Answer 1

1

In get_rain you return

return rain_inches #float

instead of

return rain_in #list
Sign up to request clarification or add additional context in comments.

1 Comment

Ahh! So easily overlooked. Will make the adjustment and hopefully not miss that again. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.