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'
4 Answers 4
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())
1 Comment
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
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
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()
students) in Python?studentsis a list. As the error tells you,listobjects do not have those methodsStudentfor 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 togetName()? 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.inputas a parameter name indef totalAverageMark(self, input):.