我正在尝试使用以下函数来模拟梁上的载荷:
def simulateBeamRun(personList, beam, times):
到目前为止,我已经想出了以下代码:
def createPersonList(fileName):
"""Function will go through each line of file and
create a person object using the data provided in
the line and add it to a list
"""
theFile = open(fileName)
next(theFile)
#array = []
for line in theFile:
aList = line.split(',')
bList = map(lambda s: s.strip('\n'), aList)
cList = [float(i) for i in bList]
print cList
def simulateBeamRun(personList, beam, times):
"""Takes a list of times covering the duration of
the simulation (0-35 s), the list of person
objects and a beam object to simulate a beam run
"""
dList = []
for time in times:
eList = []
for person in personList:
loadTuples = personModel.person.loadDisplacement(time)
if beamModel.beam.L > loadTuples[1] > 0:
eList.append(loadTuples)
else:
return None
beamModel.beam.setLoads(eList)
dList.append(beamModel.beam.getMaxDeflection())
但是,我在尝试运行该函数时遇到以下错误(在我给它任何输入之前:
for person in personList:
TypeError: 'NoneType' object is not iterable
请您参考如下方法:
为了迭代,personList 需要有一些值。
如果您使用函数createPersonList 创建personList,那么您需要return a value .否则,该列表不存在于 createPersonList 之外。
def createPersonList(fileName):
# do stuff to create cList
return cList
personList = createPersonList(myFile)
然后,personList 将具有值,您可以在后续函数中使用它。
simulateBeamRun(personList, beam, times)
如果您想在 personList 没有值的情况下完全避免运行该循环,请包含一个条件。
if personList is None:
print "No values in personList"
else:
for person in personList:
# do stuff with person
