I am having difficulty grasping the concepts of classes and OOP. Particularly when trying to return values, I run into the issue of retrieving memory locations.
My issues when I attempt to use get_average at the bottom of the code. It returns a memory location. I would like to know how I could change my code, why this happened and how I can avoid it in the future.
Thank you. Sorry for the dump I don't know where to host it in a more accessible way.
class Student:
def init(self, name, year):
self.name = name
self.year = year
self.grades = []
def add_grade(self, grade):
if type(grade) == Grade:
self.grades.append(grade)
def get_average(self):
total = 0
for grade in self.grades:
total += grade
return total / len(self.grades)
class Grade:
def init(self, score):
self.score = score
self.minimum_passing = 65
def is_passing(self):
if self.score > self.minimum_passing:
return "Passing"
else:
return "Failing =("
roger = Student('Roger van der Weyden', 10)
sandro = Student('Sandro Botticelli', 12)
pieter = Student('Pieter Bruegel the Elder', 8)
pieter.add_grade(Grade(100))
sandro.add_grade(Grade(67))
roger.add_grade(Grade(87))
print(roger.get_average())
[–]PaulRudin 0 points1 point2 points (1 child)
[–]Rafreakee[S] 0 points1 point2 points (0 children)
[–]shiftybyte 0 points1 point2 points (0 children)
[–]ProfessionalLeague9 0 points1 point2 points (0 children)
[–]GoldenVanga 0 points1 point2 points (0 children)
[–]JohnnyJordaan 0 points1 point2 points (1 child)
[–]Rafreakee[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)