appending a class to list results in appending str error python
Question: So I my program has a class with a persons name. I am trying to have the
program read it off from a input file and then read it and arrange it into
classes. While I am trying to appened the class to a list. It gives me this
error: AttributeError: 'str' object has no attribute 'append' I have used this
code before to do the same similar thing, but without reading from a file. I
was wondering what was happening to cause this error. Here is my code:

    
    
    class person:
    def __init__(self, FName, FLast,age ):
        self.FName=FName
        self.FLast=FLast
        self.age=age
    
    
    from sys import argv
    
    script = argv
    filename = raw_input('enter filename: ')
    txt = open(filename, 'r+')
    count = 0
    
    
    i = 2
    lines = txt.readlines()
    z=lines[0]
    a=lines[1]
    r=lines[2]
    hi=person(z,a,r)
    a=[hi]
    
    while i != -1:
      try:
        z=lines[i]
        a=lines[i+1]
        r=lines[i+2]
        i = i + 3
        hi=person(z,a,r)
        a.append[hi]
    except IndexError:
        i = -1
    


Answer: Few issues in your code -

  1. `a.append[hi]` \- This is not how you append, append is a function, you have to call it passing it the value to append as parameter.

  2. You are defining `a` as a string in the line - `a=lines[i+1]` \- Then you are trying to append to it.

You may want to create a new list outside the `while` , and then keep
appending to it.

Example -

    
    
    classlist = []
    while i != -1:
      try:
        z=lines[i]
        a=lines[i+1]
        r=lines[i+2]
        i = i + 3
        hi=person(z,a,r)
        classlist.append(hi)
    except IndexError:
        i = -1
    

