【问题标题】:TypeError: unsupported operand type(s) for +: 'NoneType' and 'float'TypeError:+ 不支持的操作数类型:“NoneType”和“float”
【发布时间】:2018-06-20 09:51:09
【问题描述】:
尝试将 excel 单元格数据输入的总数('123.111'、'112.1'、'123')相加,然后将它们除以计数以获得平均值。我不断收到类型错误,我不确定为什么,除非是因为单元格数据类型不兼容。如果有人可以提供帮助,还尝试计算最小值和最大值
total = 0
count = 0
for cellObj2 in rows2:
State = str(cellObj2[3].value)
if 'Queensland' in State:
newtotal = cellObj2[1].value
total = newtotal + total
print(total)
【问题讨论】:
标签:
python
excel
for-loop
【解决方案1】:
您有时会得到newtotal 和None。所以请在那里添加一个条件。喜欢
.......
if 'Queensland' in State:
newtotal = cellObj2[1].value
if newtotal:
total = newtotal + total
......
【解决方案2】:
试试这个,
有时我看到 cellObj2[1].value 为 None,所以只有在它不为空时才添加它。
total = 0
count = 0
for cellObj2 in rows2:
State = str(cellObj2[3].value)
if 'Queensland' in State and cellObj2[1].value:
total += cellObj2[1].value
print(total)