I have a spread sheet containing student names and test scores in this format:
first name, last, score
Each student can take the test up to three times, however they are given another row if they attempt the test more than once, example:
John, Smith, 80
Sally, Williams, 90
John, Smith, 100
I am trying to create Student
objects for each student and add these to a ClassOfStudents
object. But I cannot figure out how to avoid creating 'John Smith' twice. Here are the two classes:
class Student:
def __init__(self, first_name, last_name ):
self.first_name = first_name
self.last_name = last_name
self.score_first_attempt = 0
self.score_second_attempt = 0
self.score_third_attempt = 0
class ClassOfStudents:
"""Represents one class"""
def __init__(self, cohort, assignment_name):
""" intitalize empty list, will hold Student objects """
self.students = []
def add_student(self, student_obj):
self.students.append(student_obj)
and here is my main.py where I read the excel data and create objects from said data:
from student import Student, ClassOfStudents
from openpyxl import load_workbook
# intentionally left out openpxyl code but am reading excel data via the 'sheet_obj' variable
# initialize object that will hold all Student objects
class_of_students= ClassOfStudents()
# Create Student objects and add them to class_of_students
for i in range(1, 3):
first_name = sheet_obj.cell(row = i, column = 2).value
last_name = sheet_obj.cell(row =i, column = 1).value
score = sheet_obj.cell(row = i, column= 3).value
student_obj = Student(first_name, last_name) # create student object
# if there are no Student objects in class_of_students object, add the first one
if not class_of_students_obj.students:
class_of_students.add_student(student_obj)
# loop through class_of_students, if student is already included in class_of_students do not add this iterations student_obj, just discard it
for student in class_of_students_obj.students:
if student.first_name == first_name and student.last_name == last_name:
# logic for retrieving existing object and adding score_second_attempt value would go here
else:
class_of_students_obj.add_student(student_obj)
My code creates 3 Student
objects and adds them all to class_of_students
(John Smith is created twice). I believe this is because 'Sally Williams' is eventually being compared to 'John Smith', thus creating the third object. I think my attempt is approaching this in the completely wrong way. Can anyone offer a better approach to avoid creating duplicate Student
objects that represent the same physical student? Thanks for any help. (I also left out adding the score_first_attempt
value intentionally since I need to avoid duplicates before focusing on that)