Classing up Student Becomes Teacher


#1

Once we complete the Python track we can't help but look back on the earlier module and see it as having a need for better organization and structuring. This is one beginning...

class Student:
    def __init__(self, name, homework, quizzes, tests):
      self.name = name
      self.homework = homework
      self.quizzes = quizzes
      self.tests = tests

    def get_avg(self):
      return sum([0.1*avg(self.homework),0.3*avg(self.quizzes),0.6*avg(self.tests)])

# class derived driver functions

def class_avg(students):    
    return avg([student.get_avg() for student in students])

# utility functions

def avg(numbers):
    return sum(numbers) / len(numbers)

def grade(score):
    grades = {
        10: 'A', 9: 'A', 8: 'B', 7: 'C', 6: 'D',
        5: 'F',4: 'F',3: 'F',2: 'F',1: 'F',0: 'F'
    }
    return grades[score // 10]

# data

students = []
students.append(Student("Lloyd", [90, 97, 75, 92.0], [88, 40, 94.0], [75, 90.0]))
students.append(Student("Alice", [100, 92, 98, 100.0], [82, 83, 91.0], [89, 97.0]))
students.append(Student("Tyler", [0, 87, 75, 22.0], [0, 75, 78.0], [100, 100.0]))

# report

print ("\nName\tAvg\tGrade\n")

for student in students:
    print ("%s\t%.2f\t%s" %(student.name,student.get_avg(),grade(student.get_avg())))

print ("\nClass\t%.2f\t%s" % (class_avg(students), (grade(class_avg(students)))))

"Beginning" being the operative word.