Python Classes Review - Further Practice Discussion

Edit 2: I added a line of code to return a different message for a student with perfect attendance.

Edit 1: I figured out how to do what I was wanting which was to keep count of the number of absences. I converted the boolean values to integers then counted the number of ‘False’ values.

I figured out how to manipulate the different data the way I want to, I think. However, can someone please help me figure out how get my absence counter to work? It works for Pieter because he only has 1 absence, but it also says that Roger has 1 absence when he has 2. I’m sure it’s simple, but my brain is FRIED! I will edit later after I’ve worked on it some more. I think I need to review the options I have for working with the list of values in a dictionary. And I might have just answered my own question…be back soon!

class Student:
  def __init__(self, name, year):
    self.name = name
    self.year = year
    self.grades = []
    self.attendance = {}
    
  def add_grade(self, grade):
    if type(grade) == Grade:
      self.grades.append(grade.score)
    return grade
  
  def get_avg(self):
    total_grades = 0
    minimum_passing = 65
    for score in self.grades:
     total_grades += score
     average = total_grades/len(self.grades)
    if average >= minimum_passing:
      return "My grade average is: {:1f}\nI am passing! :)".format(average)
    else:
      return "My grade average is: {:1f}\nI am failing! :(".format(average)

  def add_attendance(self, date, record):
    self.attendance[date] = record
    
def get_attendance(self):
    attendance = []
    for x in self.attendance.values():  
        attendance.append(int(x))
    for x in attendance:
      if sum(attendance) == len(self.attendance):
        return "I have perfect attendance!"
      else:
        return "I have {} absence/s recorded.".format(attendance.count(0))
  
  def __repr__(self):
    return "My name is {} and I am in year {}.\nThese are my grades: {}\n{}\nAttendance Record: {}\n{}\n".format(self.name, self.year, self.grades, self.get_avg(), self.attendance, self.get_attendance())

class Grade:
  minimum_passing = 65
  def __init__(self, score):
    self.score = score

  def passing_score(grade):
    minimum_passing = 65
    if grade >= minimum_passing:
      return "This is a passing score!"
    else:
      return "You are 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))
pieter.add_grade(Grade(75))
pieter.add_grade(Grade(85))

sandro_grades = [90, 80, 95, 75, 65, 50, 45, 40, 35]
roger_grades = [95, 66, 78, 89, 98]

for x in sandro_grades:
    sandro.add_grade(Grade(x))

for x in roger_grades:
    roger.add_grade(Grade(x))

pieter_attendance = [('2/6/2021', True), ('2/5/2021', False), ('2/7/2021', True)]
sandro_attendance = [('2/6/2021', True), ('2/5/2021', True), ('2/7/2021', True)]
roger_attendance = [('2/6/2021', False), ('2/5/2021', False), ('2/7/2021', True)]

for x in list(pieter_attendance):
    date = x[0]
    record = x[1]
    pieter.add_attendance(date, record)

for x in list(sandro_attendance):
    date = x[0]
    record = x[1]
    sandro.add_attendance(date, record)

for x in list(roger_attendance):
    date = x[0]
    record = x[1]
    roger.add_attendance(date, record)  
  
#print(pieter)
#print(sandro)
#print(roger)
1 Like