Python TypeError:+;=的操作数类型不受支持:';浮动';和';列表';

Python TypeError:+;=的操作数类型不受支持:';浮动';和';列表';,python,algorithm,Python,Algorithm,这个程序是绘制一个随机游动图。我得到了下面的错误 文件“C:\Users\oicq2\Google drive\Python\Program\Test_17.py”,第104行,在 ansQuest tot+=distL[t]#每个元素数组中的值之和 TypeError:不支持+=:“float”和“list”的操作数类型 我已经在下面的行中实例化了参数 distlist,loclist=performSim(最大时间,numTrials,最大类型) 我的程序怎么了?我怎样才能纠正它 impor

这个程序是绘制一个随机游动图。我得到了下面的错误

文件“C:\Users\oicq2\Google drive\Python\Program\Test_17.py”,第104行,在 ansQuest
tot+=distL[t]#每个元素数组中的值之和
TypeError:不支持+=:“float”和“list”的操作数类型

我已经在下面的行中实例化了参数

distlist,loclist=performSim(最大时间,numTrials,最大类型)

我的程序怎么了?我怎样才能纠正它

import math, random, pylab
#pseudo random => computer simulate the randomness

class Location(object):
    def __init__(self, x, y):#x-coordinate & y-coordinate
        self.x = float(x)
        self.y = float(y)
    def move(self, xc, yc):
        return Location(self.x+float(xc), self.y+float(yc))#increment x and y, perhaps increment by 0
    def getCoords(self):
        return self.x, self.y#return a tuple of two values. 
    def getDist(self, other):
        ox, oy = other.getCoords()
        xDist = self.x - ox
        yDist = self.y - oy
        return math.sqrt(xDist**2 + yDist**2)#pythagorean theorem to compute hypotenuse, how far a point was from the origin

class CompassPt(object):#encapsulate four directions in one class in order to edit simply
    possibles = ('N', 'S', 'E', 'W')#global variable
    def __init__(self,pt):
        if pt in self.possibles: self.pt = pt
        else: raise ValueError('in CompassPt.__init__')
    def move(self, dist):
        if self.pt == 'N': return (0,dist)
        elif self.pt == 'S': return (0, -dist)
        elif self.pt == 'E': return (dist, 0)
        elif self.pt == 'W': return (-dist, 0)
        else: raise ValueError('in CompassPt.move')

class Field(object):
    def __init__(self, drunk, loc):
        self.drunk = drunk
        self.loc = loc#put the drunk in the field at that location
    def move(self, cp, dist):
        oldLoc = self.loc# the old location is the current location
        xc, yc = cp.move(dist)#compass point move get the value, eg:return(0,1),return 0 => xc, return 1 => xy
        self.loc = oldLoc.move(xc, yc)#get the move from class location
    def getLoc(self):
        return self.loc
    def getDrunk(self):
        return self.drunk

class Drunk(object):
    def __init__(self, name):
        self.name = name
    def move(self, field, cp, dist = 1):#if I dont't pass the time, use 1
        if field.getDrunk().name != self.name:#Drunk doesn't in the field
            raise ValueError('Drunk.move called with drunk not in field')
        for i in range(dist):
            field.move(cp, 1)

class UsualDrunk(Drunk):
    def move(self, field, dist = 1):
        cp = random.choice(CompassPt.possibles)# which direction to move in the subclass
        Drunk.move(self, field, CompassPt(cp), dist)#Note notation of call, which class to get move from

class ColdDrunk(Drunk):
    def move(self, field, dist = 1):
        cp = random.choice(CompassPt.possibles)
        if cp == 'S':
            Drunk.move(self, field, CompassPt(cp), 2*dist)
        else:
            Drunk.move(self, field, CompassPt(cp), dist)

class EWDrunk(Drunk):
    def move(self, field, time = 1):
        cp = random.choice(CompassPt.possibles)
        while cp != 'E' and cp != 'W':
            cp = random.choice(CompassPt.possibles)
        Drunk.move(self, field, CompassPt(cp), time)

def performTrial(time, f):
    start = f.getLoc()#wherever the drunk happens to be at this point in time in the field

    distances = [0.0]
    locs = [start]
    for t in range(1, time + 1):
        f.getDrunk().move(f)#f dot get drunk returns an object of class drunk, select the move method to move the drunk
        newLoc = f.getLoc()
        distance = newLoc.getDist(start)#how far is the new location from wherever the starting location was
        distances.append(distance)#collect the list of disctances
        locs.append(newLoc)
    return distances, locs

def performSim(time, numTrials, drunkType):#get a distance list
    distLists = []
    locLists = []
    for trial in range(numTrials):
        d = drunkType('Drunk' + str(trial))# pass a name of class
        f = Field(d, Location(0, 0))
        distances, locs = performTrial(time, f)
        distLists.append(distances)
        locLists.append(locs)
    return distLists, locLists

def ansQuest(maxTime, numTrials, drunkType, title):
    distLists, LocLists = performSim(maxTime, numTrials, drunkType)
    means = []
    distLists = performSim(maxTime, numTrials, drunkType)
    for t in range(maxTime + 1):# 1 step is necessary, 0 isn't move
        tot = 0.0
        for distL in distLists:
            tot += distL[t]# Sum of values within an array of each element
        means.append(tot/len(distLists))#divide by the number of lists
    pylab.figure()
    pylab.plot(means)
    pylab.ylabel('distance')
    pylab.xlabel('time')
    pylab.title(title +  'Ave. Distance')
    lastX = []
    lastY = []
    for locList in LocLists:
        x, y = locList[-1].getCoords()
        lastX.append(x)
        lastY.append(y)
    pylab.figure()
    pylab.scatter(lastX, lastY)
    pylab.xlabel('EW Distance')
    pylab.ylabel('NW Distance')
    pylab.title(title + 'Final locations')
    pylab.figure()
    pylab.hist(lastX)
    pylab.xlabel('EW Value')
    pylab.ylabel('Number of Trials')
    pylab.title(title + 'Distribution of Trial EW Values')

numSteps = 500
numTrials = 300
ansQuest(numSteps, numTrials, UsualDrunk, 'UsualDrunk' + str(numTrials) + 'Trials')
ansQuest(numSteps, numTrials, ColdDrunk, 'ColdDrunk' + str(numTrials) + 'Trials')
ansQuest(numSteps, numTrials, EWDrunk, 'EWDrunk' + str(numTrials) + 'Trials')
pylab.show()
distlist有一个错误,因为您有两行代码来描述一个列表。你可以评论另一个,这样你就可以得到正确的答案

distlist,loclist=performSim(最大时间,numTrials,最大类型)

distlist=performSim(最大时间、numTrials、最大类型)


tot
显然包含一个浮动
distlist
是一个列表列表,因此
dist
是一个列表。不能添加浮动和列表。
    distLists, LocLists = performSim(maxTime, numTrials, drunkType)
    means = []
##    distLists = performSim(maxTime, numTrials, drunkType)
    for t in range(maxTime + 1):# 1 step is necessary, 0 isn't move
        tot = 0.0
        for distL in distLists:
            tot += distL[t]# Sum of values within an array of each element
        means.append(tot/len(distLists))#divide by the number of lists
    pylab.figure()
    pylab.plot(means)
    pylab.ylabel('distance')
    pylab.xlabel('time')
    pylab.title(title +  'Ave. Distance')
    lastX = []
    lastY = []
    for locList in LocLists:
        x, y = locList[-1].getCoords()
        lastX.append(x)
        lastY.append(y)
    pylab.figure()
    pylab.scatter(lastX, lastY)
    pylab.xlabel('EW Distance')
    pylab.ylabel('NW Distance')
    pylab.title(title + 'Final locations')
    pylab.figure()
    pylab.hist(lastX)
    pylab.xlabel('EW Value')
    pylab.ylabel('Number of Trials')
    pylab.title(title + 'Distribution of Trial EW Values')