0% found this document useful (0 votes)
7 views1 page

Activity 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views1 page

Activity 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

class Student:

# constructor method to initialize attributes (name, age, and grades)


def __init__(self, name=None, age=None, grades=[None]):
self.__name = name
self.__age = age
self.__grades = grades

# getter and setter for the name attribute


@property
def name(self):
return self.__name

@name.setter
def name(self, name):
self.__name = name

# getter and setter for the age attribute


@property
def age(self):
return self.__age

@age.setter
def age(self, age):
self.__age = age

# getter and setter for the grades attribute


@property
def grades(self):
return self.__grades

@grades.setter
def grades(self, grades):
self.__grades = grades

# method to add a grade to the grades list


def add_grade(self, grade):
self.__grades.append(grade)

# method to calculate and return the average grade


def get_average(self):
__sum = 0
for x in self.__grades:
__sum += x
__average = __sum / len(self.__grades)
return __average

# method to display student information


def display_info(self):
print('Name: {0}\nAge: {1}\nAverage grade: {2}'.format(self.name, self.age,
self.get_average()))

# create a Student object and set its attributes


stud = Student()
stud.name = 'Trisha'
stud.age = 19
stud.grades = [1.0, 1.1, 1.2, 1.3, 1.5]

stud.add_grade(1.4)
stud.display_info()

You might also like