Python:TypeError:';非类型';对象是不可编辑的
我尝试使用以下函数模拟梁上的荷载:Python:TypeError:';非类型';对象是不可编辑的,python,Python,我尝试使用以下函数模拟梁上的荷载: 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
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
,则需要。否则,该列表不存在于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
为了进行迭代,
personList
需要包含一些值
如果您正在使用函数createPersonList
创建personList
,则需要。否则,该列表不存在于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
下面的代码是否有帮助
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"""
cList=[]#see my comments. if the following loop not happen, still return []
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]# if bList is iterable, [float(i) for i in bList] should be a list (including [])
return cList#according to your comments, should return here.
float(i)可能抛出错误,因此使用try-except。
我认为应该在这个函数中完成与personList相关的检查,应该记录错误信息。下面的代码可以帮助您吗
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"""
cList=[]#see my comments. if the following loop not happen, still return []
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]# if bList is iterable, [float(i) for i in bList] should be a list (including [])
return cList#according to your comments, should return here.
float(i)可能抛出错误,因此使用try-except。
我认为应该在这个函数中检查personList的相关信息,应该记录错误信息。
personList
是None
。缺少代码的相关部分。您需要确保personList不是未添加的相关代码。我如何使personList不是None。您应该返回cList
not print当我检查我的代码时,仅为其设置的是personList
,它是None
。您的代码的相关部分丢失。您需要确保personList不是未添加的相关代码。如何使personList不是None。您应该返回cList
而不是打印它,这正是我检查代码时才需要的