我试图使用以下函数来模拟梁上的荷载:
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
之外。然后,
personList
将有值,您可以在后续函数中使用它。如果要避免在
personList
没有值的情况下运行该循环,请包含条件。下面的代码可以帮助您吗
float(i)可能抛出错误,因此使用try except。 我认为在这个函数中应该做与personList相关的检查,应该记录错误信息。
相关问题 更多 >
编程相关推荐