1

Can you help me to solve the problem, why am I not able to call my functions? I try to calculate the average mark of all student objects created but it somehow doesnt work.

class Student:
    anz = 0

    def __init__(self, vorname, nachname, studetnId, averageMark):
        self.vorname = vorname
        self.nachname = nachname
        self.studentId = studetnId
        self.averageMark = averageMark
        Student.anz += 1

    def totalAverageMark(self, input):
        summe = 0
        for s in input:
            summe += self.averageMark
        return (summe / Student.anz)

    def getName(self, arr):
        for s in arr:
            return self.nachname

students = [Student("Maxine", "Muster", 2, 1.0), Student("Bert", "Beispiel", 1, 2.0)]

print(students.getName())
print(students.totalAverageMark())

It says: AttributeError: 'list' object has no attribute 'totalAverageMark'

6
  • 3
    Somehow? How exactly? Always include error messages and explain how your code is failing so we don't have to guess or work it out. Do you know how to use lists (such as students) in Python? Commented Nov 28, 2021 at 18:41
  • students is a list. As the error tells you, list objects do not have those methods Commented Nov 28, 2021 at 18:45
  • There is one conceptional error as well: You can ask a Student for their name (getName()), but asking one about the total average mark (totalAverageMark()) you can't expect them to return any info that includes other students. BTW: What is the meaning of the parameter to getName()? It doesn't make sense. You'd see that yourself if you made a human language description of what each method does, including the paramers. Commented Nov 28, 2021 at 18:54
  • would like to suggest not to use keywords like input as a parameter name in def totalAverageMark(self, input):. Commented Nov 28, 2021 at 18:55
  • @Ulrich Eckhardt Ok, got that! So that is why I need to create a new class or just some code outside the functions right? Commented Nov 28, 2021 at 19:09

4 Answers 4

2

Student define attributes of a single student only single. to get the collective result for example avg mark of all student, you need to create a class (that bind all students say) that defines the attribute of the class ie there students.

below is a simple implementation what you are trying to do using OOPs

class Student:
    def __init__(self, vorname, nachname, studetnId, averageMark):
        self.vorname = vorname
        self.nachname = nachname
        self.studentId = studetnId
        self.averageMark = averageMark
class Students:
    def __init__(self, students_list: list):
          self.students = students_list
    def avg_marks(self):
        total_students = len(self.students)
        marks = sum(student.averageMark for student in self.students)
        return marks/total_students

students = [Student("Maxine", "Muster", 2, 1.0), Student("Bert", "Beispiel", 1, 2.0)]
student_data = Students(students)


print(student_data.avg_marks())
Sign up to request clarification or add additional context in comments.

1 Comment

Wow, thanks a lot. I believe I can figure out what you mean.
1

students is a list of your students. List has not functions getName and totalAverageMark. You can use them only with object of Student class.

Simplest way to get total average mark of students:

class Student:
    anz = 0

    def __init__(self, vorname, nachname, studetnId, averageMark):
        self.vorname = vorname
        self.nachname = nachname
        self.studentId = studetnId
        self.averageMark = averageMark
        Student.anz += 1

students = [Student("Maxine", "Muster", 2, 1.0), Student("Bert", "Beispiel", 1, 2.0)]

sum = 0
for s in students:
    sum += s.averageMark
print(f"Total average mark is {sum / len(students)}")

9 Comments

I think it would help to expand your answer to show how to call these methods against one or more students.
Well exactly, I understand what @Ratery means. But how am I able to iterate through the list, and therefore get the total Average mark?
@MartinMüller what do you mean? Are you asking how to iterate over a list? This really has nothing to do with your stated question.
@MartinMüller I have edited my answer. You don't need class methods to do it.
So it should also be possible to write that in a method, right?
|
0

You are trying to call the methods getName() and totalAverageMark() directly on the students list object instead of accessing each individual Student objects within the list.

Iterate over each Student object in the students list and call the methods on each object:

class Student:
    anz = 0

    def __init__(self, vorname, nachname, studentId, averageMark):
        self.vorname = vorname
        self.nachname = nachname
        self.studentId = studentId
        self.averageMark = averageMark
        Student.anz += 1

    @staticmethod
    def totalAverageMark(students):
        summe = 0
        for student in students:
            summe += student.averageMark
        return summe / len(students)

    def getName(self):
        return self.nachname

students = [Student("Maxine", "Muster", 2, 1.0), Student("Bert", "Beispiel", 1, 2.0)]

average_mark = Student.totalAverageMark(students)
print(f"Average Mark: {average_mark}")

for student in students:
    name = student.getName()
    print(f"Student Name: {name}")

Comments

0

To iterate through the list and print out the result for each student you can use the following:

for student in students:
   print(student.getName())
   print(student.totalAverageMark())

Or to access an individual student

students[0].getName()

1 Comment

Yes, but no: This won't run. Also, it won't give the average mark of all students.

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.