0

I am writing some code for a project at school. I am reading in a List that I have created as a text file with 5 attributes. This is my class object code:

class studentclass(object):
    def __init__(self,firstname,lastname,classno,correct,mydate):
        self.firstname = firstname
        self.lastname = lastname
        self.classno = classno
        self.correct = correct
        self.mydate = mydate

Later in the program I am using this code to read in the data, sort it and perform some calculations:

myList = [studentclass]
totalnoofrecords = 0
counter = 0

for counter in range(0,totalnoofrecords):
    firstname = myList.firstname[counter]
    lastname = myList.lastname[counter]
    classno = myList.classno[counter]
    correct = myList.correct[counter]
    mydate = myList.mydate[counter]
    newname = myList.firstname[counter +1]
    if newname == firstname:
            grade = grade + studentclass.correct(counter +1)
            nummberofattempts = 2
    newname2 = studentclass.firstname(counter +2)
    if newname2 == firstname:
            grade = grade + studentclass.correct(counter +2)
            nummberofattempts = 3
    mean = grade / numberofattempts

    print ("num ",counter ,"=", myList[counter])

But it does not work. I get the following error message:

AttributeError: 'list' object has no attribute 'firstname'

The error message points to this line of the code:

firstname = myList.firstname[counter]

Hoping that someone call help me please. Thanks

1
  • Your myList is a list, not a class instance. Lists have no attributes. Commented Dec 6, 2015 at 22:58

2 Answers 2

1

In your code you are referencing mylist.firstname. What is mylist? It's a list. Does it have a firstname attribute? The error is telling you that it doesn't, and looking at the code you aren't adding that attribute to the list.

Each element of the list has that attribute, however. Perhaps you meant to get the firstname attribute of one of the elements of the list. Maybe the following, perhaps?

for counter in range(0,totalnoofrecords):
    firstname = myList[counter].firstname
    lastname = myList[counter].lastname
    ...

In python, when you get an error like "object X has no attribute Y", you can usually rely on that being a true statement. So, ask youself "why does X not have that attribute?". It's usually either a) you forgot to define that attribute, b) you misspelled the attribute or you misspelled X, or c) X isn't what you think it is.

Sign up to request clarification or add additional context in comments.

1 Comment

Good answer, except you should iterate directly over my list, not over a range.
1

You have several issues. As Alex S. pointed out, your myList is a List, and specifically it is a list with one element: a class constructor.
I think what you want is something like:

  # assumption: you have textlines, 
  # which is an array of lines of the form firstname,lastname,blah
  myList = [studentclass(*(args.split(",")) for args in textlines]

And then do myList[counter].firstname to get the (counter-th) firstname value

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.