Martin Müller
Martin Müller

Reputation: 41

Python beginner OOP functions

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'

Upvotes: 1

Views: 108

Answers (4)

Corr
Corr

Reputation: 3

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

Upvotes: 0

kit4py
kit4py

Reputation: 90

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}")

Upvotes: 0

sahasrara62
sahasrara62

Reputation: 11238

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

Upvotes: 2

Ratery
Ratery

Reputation: 2917

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)}")

Upvotes: 1

Related Questions