简体   繁体   中英

Python Rainfall program TypeError: unsupported operand type(s) for +: 'int' and 'str'

So I am getting this error: Traceback (most recent call last): File "C:/Users/", line 8, in total = total+x TypeError: unsupported operand type(s) for +: 'int' and 'str'

array = []
total = 0
max = 0
min = 0
for i in range(12):
    x= input("Enter rainfall for each of the 12 months:")
array.append(x)
total = total+x
if array[max]< x:
    max = i
if array[min] > x:
    min = i
print("The total rainfall in the year is ", total)
print("The average rainfall for the year is ", total / 12.0)
print("Month number ", max + 1, " has the highest rainfall which is ", array[max])
print("Month number ", min + 1, " has the lowest rainfall which is ", array[min])

Your x from input is str not int . Just convert it to int before using it:

x= input("Enter rainfall for each of the 12 months:")
x = int(x)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM