New teacher in town

I’m working on the learn intermediate python 3 module.
I’m stuck on step 3 of the New Teacher in Town project. The step wants a list of the students. The list is to be alphabetically listed by their first names. The step says to create a Classroom Organizer class to create the list. After I set up the class, I am having difficulty figuring how to proceed.
Should I create a blank list of of names so I can append it with the names?
Here’s my code so far.

Screenshot Capture - 2024-05-26 - 18-14-48
Screenshot Capture - 2024-05-26 - 18-15-05

It looks like the class is already defined in the classroom_organizer module that is imported on line 3. It might be all we need is an instance of that class.

Is this the exercise you are working on?

https://www.codecademy.com/courses/learn-intermediate-python-3/projects/new-teacher-in-town-project

If so, then our earlier speculation was correct. The class already exists. We only need an instance. Not sure if the version of ‘classroom_organizer’ that I have is the same as what you are given. Please post the code from that module so we are working from the same code and not giving you errant information.

Yes, that is the project I’m working on.
My code is below. It’s also in the screenshots as well.

from roster import student_roster
import itertools
import classroom_organizer
student_roster_iterator = iter(student_roster)
lass ClassroomOrganizer:
index = 0
def init(self):
self.sorted_names = self._sort_alphabetically(student_roster)
ClassroomOrganizer.index += 1
def iter(self):
sort_alphabetically()
print(names)
return self
def _sort_alphabetically(self,students):
names =
for student_info in students:
name = student_info[‘name’]
names.append(name)
return sorted(names)
_sort_alphabetically()

You may still have time to edit that post and format the code using the </> button in the tool tray once you select all the code.

We also need the code from the ‘classroom_organizer’ module. Click the folder icon in the LE to expose the module/file list, then click that .py file and click the X above to close that pane. The file will now be a tab in the editor window header. Access that tab and Ctrl-A Ctrl-C. then paste it into your reply. Be sure to format it.

Thanks, Below is the code from the classroom organizer file.

Import modules above this line

class ClassroomOrganizer:
def init(self):
self.sorted_names = self._sort_alphabetically(student_roster)

def _sort_alphabetically(self,students):
names =
for student_info in students:
name = student_info[‘name’]
names.append(name)
return sorted(names)

def get_students_with_subject(self, subject):
selected_students =
for student in student_roster:
if student[‘favorite_subject’] == subject:
selected_students.append((student[‘name’], subject))
return selected_stud

You still need to learn how to post code samples in the forums. Please look into this. Edit your posts if you still can. We really do need to know where the indentation is, especially on your code. The above we can safely assume it.

There is also a “Copy to Clipboard” button at the bottom of exercises/projects. After copying your code, you can use the </> button while posting on the forums so that the formatting is preserved.

I think i figured it out. Below is the code:

from roster import student_roster
import itertools
import classroom_organizer
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))

class ClassroomOrganizer:
  def __init__ (self, sorted_names):
    self.sorted_names = sorted_names
  def __getitem__(self, key):
      return self.sorted_names[key]
  sorted_names = student_roster_iterator['name']
  def __init__(self, student_roster):
    self.student_roster = sorted(student_roster)
    self.index = -1
  def __iter__(self):
    return self
  def _next__(self):
    self.index += 1
    if self.index < len(self.students):
      return self.students[self.index]
    raise StopIteration
classroom_organizer = ClassroomOrganizer(student)
for student in classroom_organizer:
  print(student)


1 Like

Glad you figured how to post formatded code so we can see the blocks. Is that your code from script.py? What about the classroom_organizer.py script?

Too bad you chose to duck out. This is a very difficult project to figure out and practically impossible to outline. The author completely dropped the ball on this one and nobody every held them to account. I’ve never seen a finished version of this project that made any sense.

If you have the willingness, we can take this opportunity to try to once and for all figure out what the course author had in mind, and was trying to convey into some sort of meaningful instruction. I found myself chasing my tail and only out of spite tried to learn something from this. Given there is no model to check my work against. it has stood in limbo these past five or ten years in a state of incompleteness, but that gives some results we can find interesting.

The hint hanging in the air is that the core of the project is iterators. A piss poor way to show it, especially hiding behind the skirts of parent, CC without ever presenting a working example learners could learn from and work toward (or just copy and move on, at their own peril). Instruction is pointless if there is no takeaway. This project has nothing of a takeaway, only a lot of unanswered questions.

No point complaining today, as the author of this course is long gone and this product is in limbo on the shelf for us to pick apart to try and render something sensible from it that we CAN take away.

1 Like

THe classroom organizer code is:


# Import modules above this line
class ClassroomOrganizer:
  def __init__(self):
    self.sorted_names = self._sort_alphabetically(student_roster)

  def _sort_alphabetically(self,students):
    names = []
    for student_info in students:
      name = student_info['name']
      names.append(name)
    return sorted(names)

  def get_students_with_subject(self, subject):
    selected_students = []
    for student in student_roster:
      if student['favorite_subject'] == subject:
        selected_students.append((student['name'], subject))
    return selected_students`Preformatted text`

The script.py code is:

from roster import student_roster
import itertools
import classroom_organizer
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))

class ClassroomOrganizer:
  def __init__ (self, sorted_names):
    self.sorted_names = sorted_names
  def __getitem__(self, key):
      return self.sorted_names[key]
  sorted_names = student_roster_iterator['name']
  def __init__(self, student_roster):
    self.student_roster = sorted(student_roster)
    self.index = -1
  def __iter__(self):
    return self
  def _next__(self):
    self.index += 1
    if self.index < len(self.students):
      return self.students[self.index]
    raise StopIteration
classroom_organizer = ClassroomOrganizer(student)
for student in classroom_organizer:
  print(student)


We have a ClassroomOrganizer class in the imported module so won’t be needing to define the class again. If you wish to add to the definition, modify the module file, not the script.py file.

If we go back to instruction 1,

s = iter(student_roster)    #  s becomes an iterator object
while True:
  try:
    print (next(s))
  except StopIteration:
    break
{'name': 'Karina M', 'age': 8, 'height': 48, 'favorite_subject': 'Math', 'favorite_animal': 'Dog'}
{'name': 'Yori K', 'age': 7, 'height': 50, 'favorite_subject': 'Art', 'favorite_animal': 'Cat'}
{'name': 'Alex C', 'age': 7, 'height': 47, 'favorite_subject': 'Science', 'favorite_animal': 'Cow'}
{'name': 'Esmeralda R', 'age': 8, 'height': 52, 'favorite_subject': 'History', 'favorite_animal': 'Rabbit'}
{'name': 'Sandy P', 'age': 7, 'height': 49, 'favorite_subject': 'Recess', 'favorite_animal': 'Guinea Pig'}
{'name': 'Matthew Q', 'age': 7, 'height': 46, 'favorite_subject': 'Music', 'favorite_animal': 'Walrus'}
{'name': 'Trudy B', 'age': 8, 'height': 45, 'favorite_subject': 'Science', 'favorite_animal': 'Ladybug'}
{'name': 'Benny D', 'age': 7, 'height': 51, 'favorite_subject': 'Math', 'favorite_animal': 'Ant'}
{'name': 'Helena L', 'age': 7, 'height': 53, 'favorite_subject': 'Art', 'favorite_animal': 'Butterfly'}
{'name': 'Marisol R', 'age': 8, 'height': 50, 'favorite_subject': 'Math', 'favorite_animal': 'Lion'}

I;'m able to get the list to print. The questions is how to sort the names alphabetically.

The code is below:

from roster import student_roster
import itertools
import classroom_organizer
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))
class ClassroomOrganizer:
  def __init__ (self, sorted_names):
    self.sorted_names = sorted_names
  def __getitem__(self, name):
      return self.sorted_names[name]
  sorted_names = student_roster_iterator['name']
  def __init__(self, student_roster):
    self.student_roster = student_roster
    self.index = -1
  def __iter__(self):
    return self
  def _next__(self):
    self.index += 1
    if self.index < len(self.students):
      return self.students[self.index]
    raise StopIteration
classroom_organizer = ClassroomOrganizer(student_roster)
for name in classroom_organizer:
  print(student)


The error code I'm getting is:
Traceback (most recent call last):
  File "script.py", line 15, in <module>
    class ClassroomOrganizer:
  File "script.py", line 20, in ClassroomOrganizer
    sorted_names = student_roster_iterator['name']
TypeError: 'list_iterator' object is not subscriptable

It’s already done for us. Consider the new instance, below,

s = ClassroomOrganizer()

s has one attribute of note, sorted_names. It is created when we instantiate the object.

It’s not working for me. What am I doing wrong?

from roster import student_roster
import itertools
import classroom_organizer
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))
class ClassroomOrganizer:
  def __init__(self, student_name):
    self.sorted_names = self._sort_alphabetically(student_roster)
  def _sort_alphabeticcally(self, students):
      names = []
      for student in student_info:
        name = student_info['name']
        names.append(name)
      return sorted(names)
  def __init__(self, student_name):
    self.sorted_names = self._sort_alphabetically(student_roster)
  def __iter__(self):
    return self
  def __next__(self):
    if self.student_index >= len(self.sorted_names):
      raise StopIteration
    student_name = self.sorted_names[self.current_index]
    self.current_index +=1
    return student_name
s = ClassroomOrganizer(student_roster)   #  s becomes an iterator object
while True:
  try:
    print (next(sorted(s)))
  except StopIteration:
    break`Preformatted text`

We don’t pass anything into the ClassroomOrganizer constructor. We call it with no argument and expect a sorted_names attribute on our instance.

r = ClassroomOrganizer()
print (r.sorted_names)

s = iter(r.sorted_names)
while True:
    try:
        print (next(s))
    except StopIteration:
        break

As is normal behavior for iterators, s will now be the empty list, [] having been consumed in the loop.

>>> print (s)  # s is still a list_iterator object
    <list_iterator object at 0x000002BB1D9F05B0>
>>> print ([*s])  # running the iterator again
    []
>>> 

This will have no effect on r.sorted_names.

Note that r will inherit two methods from the parent class, and if we wish to add more methods, we can, but it is in the classroom_organizer module that we perform those changes, not in script.py.

I tried the code you gave me. Below is the error message I got:

Traceback (most recent call last):
File “script.py”, line 34, in
r = ClassroomOrganizer()
TypeError: init() missing 1 required positional argument: 'student_name

You should not have the ClassroomOrganizer class definition in script.py. It is imported:

from classroom_organizer import ClassroomOrganizer
from roster import student_roster

Comment out all the code you have for that in script.py from

class ClassroomOrganizer:

down to the bottom. You can run my code from above right under the import lines above.

You can add student names using the add_student method and then retrieve the sorted list using the get_sorted_student_names method.