1

I am attempting to use the following function to simulate loads on a beam:

def simulateBeamRun(personList, beam, times):

I have come up with the following code so far:

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())

However, I am getting the following error when trying to run the function (before I give it any inputs:

for person in personList:
TypeError: 'NoneType' object is not iterable
10
  • 5
    personList is None. Relevant parts of your code are missing. Commented Jan 5, 2016 at 16:46
  • 1
    You need to make sure that personList is not None Commented Jan 5, 2016 at 16:47
  • Added relevant code. How would I make personList not None. Commented Jan 5, 2016 at 16:53
  • You should be returning cList not printing it Commented Jan 5, 2016 at 16:54
  • That was just for when I was checking my code Commented Jan 5, 2016 at 16:56

2 Answers 2

5

In order to be iterated, personList needs to have some values in it.

If you're creating personList with the function createPersonList, then you need to return a value. Otherwise, that list doesn't exist outside of createPersonList.

def createPersonList(fileName):
    # do stuff to create cList
    return cList

personList = createPersonList(myFile)

Then, personList will have values and you can use it in subsequent functions.

simulateBeamRun(personList, beam, times)

If you want to avoid running that loop at all in cases where personList doesn't have values, include a conditional.

if personList is None:
    print "No values in personList"
else:
    for person in personList:
        # do stuff with person
Sign up to request clarification or add additional context in comments.

Comments

2

May the following code help

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) may throw errors, so use try-except. I think checking related to personList should be done in this function, error info should be logged.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.