首页 > 解决方案 > Python Rainfall程序TypeError:+的不支持的操作数类型:'int'和'str'

问题描述

所以我收到了这个错误: Traceback(最近一次调用最后一次):文件“C:/Users/”,第 8 行,总计 = 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])

标签: pythonarrays

解决方案


您的xfrom 输入str不是int. 只需int在使用前将其转换为:

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

推荐阅读