Step 5 in new teacher in town

I’m working on the new teacher in town project in the intermediate python 3 course.
I’m stuck on step 5 which asks you to create an after school program for students whose favorite subjects are science and math.
I tried to set up step5 similar to step 3 which asks you to sort the names of the students alphabetically. When I tried to set it up for step 5, I keep getting a message saying:
Traceback (most recent call last):
File “script.py”, line 72, in
classroom = ClassroomOrganizer1()
TypeError: init() missing 1 required positional argument: ‘subject’

Below is my code:

from roster import student_roster
import itertools
from classroom_organizer import ClassroomOrganizer
student_roster_iterator = iter(student_roster)
#print(next(student_roster_iterator))
#print(next(student_roster_iterator))
#print(next(student_roster_iterator))
#print(next(student_roster_iterator))
#print(next(student_roster_iterator))
#print(next(student_roster_iterator))
#print(next(student_roster_iterator))
#print(next(student_roster_iterator))
#print(next(student_roster_iterator))
#print(next(student_roster_iterator))

import itertools
from roster import student_roster

# Import modules above this line
class ClassroomOrganizer:
  def __init__(self):
    self.sorted_names = self._sort_alphabetically(student_roster)
    self.counter = 0
  def __iter__(self):
      return self
  def __next__(self):
    if self.counter < len(student_roster):
      nxt_value = self.sorted_names[self.counter]
      self.counter += 1
      return nxt_value
    else:
      raise StopIteration

  def _sort_alphabetically(self,students):
    names = []
    for student_info in students:
      name = student_info['name']
      names.append(name)
    print(sorted(names))
class ClassroomOrganizer1:
  def __init__(self, subject):
    self.sorted_subjects = self.get_students_with_subjects
    self.counter = 0
  def __iter__(self):
    return self
  def __next__(self):
    if self.counter < len(student_roster):
      nxt_value = self.sorted_names[self.counter]
      self.counter += 1
      return nxt_value
    else:
      raise StopIteration
  def get_students_with_subject(self, students, subject):
    selected_students_subjects = []
    for student_info in students:
      selected_student = student_info['name']
      favorite_subject = student_info['favorite_subject']
    selected_students_subjects.chain(selected_student, favorite_subject)
    print(selected_students)
classroom = ClassroomOrganizer()


tables = ['Table1', 'Table2', 'Table3', 'Table4', 'Table5']
table_iterator = iter(tables)

table_combo = itertools.combinations(tables, 2)
students_list = ['Alex', 'Benny', 'Esmeralda', 'Helena', 'Karina', 'Marisol', 'Matthew', 'Sandy', 'Trudy', 'Yori']
student_list_iterator = iter(students_list)
student_combo = itertools.combinations(students_list, 2)
for combo in student_combo:
  print(combo)
classroom = ClassroomOrganizer1()

1 Like

You seem to be having trouble with classes and methods in Python.

Notice that you have

from classroom_organizer import ClassroomOrganizer

meaning that the ClassroomOrganizer class came from the file classroom_organizer.py,
so you should have the ClassroomOrganizer class (and methods) created only in the classroom_organizer.py file, instead of in script.py (meaning you’ll also have to go to the tab for classroom_organizer.py and change code there).

In your case, I think that you’re missing an argument for ClassroomOrganizer1 based on the error you posted.
I guess that
ClassroomOrganizer1()
should be
ClassroomOrganizer1("Science")
so that you get the students whose favorite subject is “Science”.

In the __init__ method of ClassroomOrganizer1, you have
self.sorted_subjects = self.get_students_with_subjects
but it seems like self.sorted_subjects should be a list, but self.get_students_with_subjects
is a method (a function in the class).
To fix that, you’d have to do something like
self.get_students_with_subject(student_roster, subject)
so that .get_students_with_subject has the appropriate arguments.
Also, self.sorted_subjects does not return anything, so self.sorted_subjects would be None.
You can fix that by having
return selected_students
at the end of the get_students_with_subject method.

In ClassroomOrganizer1, you have self.sorted_subjects in the __init__ method,
but you have self.sorted_names in the __next__ method.

In your methods, you have print in several places where I had return or yield from in my code.

THanks for the hellp. I was able to figure it out.