【发布时间】:2020-09-18 21:43:17
【问题描述】:
我试图在一个类中放置一个方法,该方法将用非空值的运行平均值替换空值。请参考以下代码:
class ElNinoData(object):
# Your implementation here
add = 0
counter = 0
average = 0
# methods
def __init__(self , object):
self.object = object
def get_humidity(self):
if not math.isnan(self.object['humidity']):
global add
global counter
global average
add += self.object['humidity']
counter += 1
average = add/counter
else:
self.object['humidity'] = average
return self.object['humidity']
在方法中执行此类时,我收到以下错误:
<ipython-input-40-c52c3ac6484b> in get_humidity(self)
19 global average
20
---> 21 add += self.object['humidity']
22 counter += 1
23 average = add/counter
NameError: name 'add' is not defined
谁能解释一下这个错误,我对python比较陌生?
【问题讨论】:
标签: python python-3.x oop