1

Having an issue with a certain part of the code (I am new to coding and have tried looking through StackOverflow for help):

def totalRainfall (rainfall):
    totalRain = 0
    month = 0
    while month < len(rainfall):
        totalRain = rainfall[month] + totalRain
        month += 1

    return totalRain

TypeError: Can't convert 'int' object to str implicitly

I've tried multiple ways of changing the code to make it a string explicitly as it still giving me various issues.

I'm also having a hard time enhancing the code to sort the array in ascending order and displays the values it contains.

The full code is here:

def main ():
    rainfall = rainInput ()
    totalRain = totalRainfall (rainfall)
    average_Rainfall = averageRainfall (totalRain)
    highestMonth, highestMonthly = highestMonthNumber (rainfall)
    lowestMonth, lowestMonthly = lowestMonthNumber (rainfall)
    print #this is for spacing output
    print ('The total rainfall for the year was: ' +str(totalRain) + ' inche(s)')
    print #this is for spacing output
    print ('The average rainfall for the year was: ' +str(average_Rainfall) +\
          ' inche(s)') 
    print #this is for spacing in output
    print ('The highest amount of rain was', highestMonthly, 'in' , highestMonth)
    print #this is for spacing in output
    print ('The lowest amount of rain was', lowestMonthly, 'in' , lowestMonth)

def rainInput ():
    rainfall = ['January','Febuary','March','April','May','June','July','August'\
    ,'September','October','November','December']
    month = 0
    while month < len(rainfall):
        rainfall[month] = input ('Please enter the amount for month ' + str\
        (month + 1) + ': ')
        month += 1

    return rainfall

def totalRainfall (rainfall):
    totalRain = 0
    month = 0
    while month < len(rainfall):
        totalRain = rainfall[month] + totalRain
        month += 1

    return totalRain

def averageRainfall (totalRain):
    average_Rainfall = totalRain / 12

    return average_Rainfall

def highestMonthNumber (rainfall):
    month = ['January','Febuary','March','April','May','June','July','August'\
                ,'September','October','November','December']
    highestMonthly = 0
    for m, n in enumerate(rainfall):
        if n > highestMonthly:
            highestMonthly = n
            highestMonth = m

    return month[highestMonth], highestMonthly

def lowestMonthNumber (rainfall):
    month = ['January','Febuary','March','April','May','June','July','August'\
                ,'September','October','November','December']
    lowestMonthly = 0
    for m, n in enumerate(rainfall):
        if n < lowestMonthly:
            lowestMonthly = n
            lowestMonth = m

    return month[lowestMonth], lowestMonthly

main()

2 Answers 2

2

You have stored strings in your array rainfall, you need to convert them to ints before adding.

def totalRainfall (rainfall):
    totalRain = 0
    month = 0
    while month < len(rainfall):
        totalRain = int(rainfall[month]) + totalRain
        month += 1

    return totalRain
Sign up to request clarification or add additional context in comments.

Comments

1

If you want the total rainfall as the sum of the numbers per month, simply use sum() on the list of ints. But as your error suggests, you have a list of strings, which you explicitly have to convert.

Something along the lines of

def totalRainfall (rainfall):
    return sum([int(x) for x in rainfall])

The problem with your list being strings will continue to be problematic for you, so as a quick fix, I suggest you change this line

    rainfall[month] = input ('Please enter the amount for month ' + str\
    (month + 1) + ': ')

to

    rainfall[month] = int(input('Please enter the amount for month ' + str\
    (month + 1) + ': '))

That way your list contains only numbers and all your other comparisons will work.

You should also add this initialization in your lowestMonthNumber function to avoid UnboundLocalError: local variable 'lowestMonth' referenced before assignment:

lowestMonth = 0

Notice, that by initializing lowestMonthly to 0, you will most likely never get a correct result, since it is highly unlikely that any month has less rainfall.

2 Comments

Thank you this helped a lot. Still having a hard time grasping some of the concepts. Would you also be able to help out with this: I'm also having a hard time enhancing the code to sort the array in ascending order (alphabetical) and displays the values it contains.
sorted(), possibly with a custom key-function? sorted(['c', 'a', 'A', 'b']) -> ['A', 'a', 'b', 'c']

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.