Python:AttributeError:“int”对象在传递用户定义的对象时没有属性错误

Python:AttributeError:“int”对象在传递用户定义的对象时没有属性错误,python,python-2.7,attributes,pycharm,anaconda,Python,Python 2.7,Attributes,Pycharm,Anaconda,我有一个麦克风类,其结构如下: class Microphone(object): # Microphone class def __init__(self, x, y, limit): self.X = x self.Y = y self.low_limit = limit self.counter = 0 以及一个用于处理麦克风实例列表的函数 def knock_calibration_2d(port, microphones, W): print "

我有一个麦克风类,其结构如下:

class Microphone(object):  # Microphone class

def __init__(self, x, y, limit):
    self.X = x
    self.Y = y
    self.low_limit = limit
    self.counter = 0
以及一个用于处理麦克风实例列表的函数

def knock_calibration_2d(port, microphones, W):

print " ------------------------- Knock calibration ----------------------"
i = 0
while True:
    if port.in_waiting > 0:
        msg = str(port.readline()).strip()
        if msg.startswith("ACK:"):  # received an ACK message
            continue
        elif msg.startswith("A"):  # received an ADC value
            words = msg.split(' ')
            microphones[(ord(msg[1]) - ord('0'))] = int(words[1])
            i = i + 1
        if i == len(microphones):
            break

print "Raw calibration counter values: " + str([e for e in microphones])

L = W / math.sqrt(2)  # W=47
T = 0.0

for j in range(1, len(microphones)):
    T = T + (microphones[j].counter - microphones[0].counter)
T = T / (len(microphones) - 1)
C = L / T
print "Normalized calibration counter values: " + str([e for e in microphones])
print "L=" + str(L) + " T=" + str(T) + " C=" + str(C)

return 1
使用下面的表达式,我总是会得到一个AttributeError:“int”对象没有属性“counter”错误消息,同时,我非常确定麦克风列表只包含麦克风对象,而不是int,int具有counter属性。这里有什么问题? 我用完整的代码创建了一个粘贴库。我正在使用pycharm、anaconda和python2.7解释器

T=T+话筒[j]。计数器-话筒[0]。计数器

以下是我定义列表的主要函数:

def main():

microphones = [
                Microphone(  0,    0,   50),
                Microphone(  W/2,  W/2, 50),
                Microphone(  W/2, -W/2, 50),
                Microphone( -W/2, -W/2, 50),
                Microphone( -W/2,  W/2, 50)
               ]

C = knock_calibration_2d(port, microphones, W)

在敲打校准2d函数中,行 10~12,更换话筒元件

您用一个
Int对象,如果消息以“A”开头

谢谢!为我节省了很多时间。正确的版本是:麦克风[ordmsg[1]-ord'0']。计数器=输入字[1]