python - 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])
解决方案
您的x
from 输入str
不是int
. 只需int
在使用前将其转换为:
x= input("Enter rainfall for each of the 12 months:")
x = int(x)
推荐阅读
- python - 如何在 Python 中返回整个字符串并从中提取一列?
- regex - Firefox 给出 SyntaxError: invalid regexp group
- angular - Ionic 3 open modal 出现未在入口组件中声明但已存在的错误
- c# - 如何在 C# 中的 ListView 上运行 Linq 查询?
- sql-server-2012 - Cakephp 3 与 SQL Server 2012
- rust - 为什么这个迭代器在不直接作为函数参数调用时会丢弃?
- python-3.x - 如何计算 3 通道图像中的黑色像素图像?
- css - 如何在 WordPress 网站上定义元素的粘性位置
- json - react-native:使用 async/await 和 setState
- c++ - 如何修复 CMake 错误:源目录