class Student:
def __init__(self, first_name, last_name, grade_level):
self.first_name = first_name
self.last_name = last_name
self.grade_level = grade_level
self.is_enrolled = True
self.grade_list = []
def promote(self):
self.grade_level += 1
def get_average(self):
return sum(self.grade_list)/len(self.grade_list)
def add_grade(self, grade):
self.grade_list.append(grade)
def __str__(self):
return self.first_name + " " + self.last_name
To make an object, you must call the constructor:
bob = Student("Bob", "Jones", 9)
Now bob is a Student object, with the following initialized variables:
bob.first_name = "Bob"
bob.last_name = "Jones"
bob.grade_level = 9
bob.is_enrolled = True
bob.grade_list = []
These variables can be accessed and changed.
print(bob.first_name) will print "Bob"
bob.is_enrolled = False will change bob's enrollment status to False
You call a method in the same way you access a variable, but this time with parentheses and any appropriate arguments.
For example bob.add_grade(97) would add the grade 97 to bob's grade_list.