Esiswildflower
Esiswildflower

Reputation: 21

unexpected TypeError: unsupported operand type(s) for +=: 'int' and 'str'

The task is to write a program which asks the user to enter the total rainfall for each of the 12 months. The inputs will be stored in a list. The program should then calculate and display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest amounts.

I am supposed to do this by using a loop that loops 20 times and appends each score to a list after it is entered. Please keep in mind that I am a beginner. Here's my code so far:

def main():

        months = [0] * 12
        name_months = ['Jan','Feb','Mar','Apr','May','Jun', \
                   'Jul','Aug','Sep','Oct','Nov','Dec']

        def total(months):
            total = 0
            for num in months:
                total += num
            return total

        for index in range(12):
            print('Please enter the amount of rain in')
            months[index] = input(name_months[index] + ': ')
        print('The total is'), total(months),'mm.'


        avarage = total(months) / 12.0
        print('The avarage rainfall is'), avarage,'mm.'

main()

Upvotes: 0

Views: 2462

Answers (3)

timgeb
timgeb

Reputation: 78790

Then the teacher asks to use a loop that loops 20 times and appends each score to a list after it is entered.

The 20 has got to be a typo unless you are supposed to use a very weird calendar. :)

Reason for the error:

You are initializing total to 0 but then try to add strings to it because input will return a string. You will have to convert your input to integer (or float) values first.

Suggestions to clean up your program:

months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
am = [int(input('input rainfall for {}: '.format(m))) for m in months]
comb = list(zip(months,am))
total = sum(am)
avg = total/12
least = min(comb, key=lambda x: x[1])
most = max(comb, key=lambda x: x[1])

print('month\tamount')
for mon, amount in comb:
    print('{0}\t{1}'.format(mon, amount))

print('total: {}'.format(total))
print('average: {}'.format(avg))
print('least rain in: {0} ({1})'.format(least[0], least[1]))
print('most rain in: {0} ({1})'.format(most[0], most[1]))

sample run:

input rainfall for Jan: 1
input rainfall for Feb: 2
input rainfall for Mar: 3
input rainfall for Apr: 4
input rainfall for May: 5
input rainfall for Jun: 6
input rainfall for Jul: 7
input rainfall for Aug: 8
input rainfall for Sep: 9
input rainfall for Oct: 10
input rainfall for Nov: 11
input rainfall for Dec: 12
month   amount
Jan     1
Feb     2
Mar     3
Apr     4
May     5
Jun     6
Jul     7
Aug     8
Sep     9
Oct     10
Nov     11
Dec     12
total: 78
average: 6.5
least rain in: Jan (1)
most rain in: Dec (12)

Upvotes: 1

Dietrich Epp
Dietrich Epp

Reputation: 213827

This must be Python 3. You have to convert the user input to numbers instead of strings:

# Sets months[index] to a string
months[index] = input(name_months[index] + ': ')

Should be:

# Sets months[index] to a (floating-point) number
months[index] = float(input(name_months[index] + ': '))

Upvotes: 2

Christian Berendt
Christian Berendt

Reputation: 3556

It is not possible to concatenate a String with an Integer using the + operator. You have to convert the Integer into a String using str(int).

>>> 10 + '10'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'

Upvotes: -2

Related Questions