New Teacher in Town

Having trouble with imported items in course
Script.py is not recognizing an object from classroom_organizer.py after importing.
on assignment number 4

4:“Next, we need to organize our classroom and decide where our students will be sitting. We have 5 tables in our classroom that can seat 2 students each. We’d like to see what combination of students we can put at each table.
Using the itertools module, define a method within ClassroomOrganizer that will retrieve a final list of all tuple combinations of 2 students that can be seated at each table.
From script.py, print out the result to see all the possible combinations.”

current issue is that script.py is saying “two_students” is not defined when it is defined in ClassroomOrganizer.py did I import it incorrectly?

You can find gist here: gist

I just tried adding global above “two_students” still getting not defined.

Also your comment " Also, I don’t see that function perTable() called anywhere.
I assume its a static method: ClassroomOrganizer.perTable()" doesn’t make sense to me? I don’t see why it would need to be called/

I checked … global does not work here because it can only access a variable, but not modify it (in this case).

The function per_table needs to be called because the code in the function won’t run otherwise. (It’ll compile, but it won’t get executed.)

Also, if you want to get the contents of that variable two_students from the function, you may want to return it (or some version of it) or just have the print inside the function.

Here’s one possibility …

Inside the ClassroomOrganizer class:

    def per_table():
      two_students = itertools.combinations(student_roster, 2)
      return list(two_students)

In script.py:

print( ClassroomOrganizer.per_table() )

This worked and I moved on to number 5 which is:

You are offering an afterschool program for those students whose favorite subjects are Math and Science.

Your tables can fit 4 students at them. Retrieve a list of all 4 combinations of students whose favorite subjects are Math and Science.

The get_students_with_subject() method can be used to retrieve iterables for each of the subjects.

From script.py , print the final list of combinations.

I am assuming I would set up something like this inside ClassroomOrganizer.py

def math_and_science():
      chained_iterables = itertools.chain(math, science)
      combinations_iterables = itertools.combinations(chained_iterables, 4)
      return list(combinations_iterables)
    math_and_science()

then print combinations_iterables?

Sure, you can do that as a function,
but the function call
math_and_science()
or
ClassroomOrganizer.math_and_science() (if the function is in that class)
would be in script.py

print( ClassroomOrganizer.math_and_science() )

Don’t forget the other 2 lines that go before chained_iterables:

math = class1.get_students_with_subject("Math")
science = class1.get_students_with_subject("Science")

Sorry to ask yet another question, but you say that the function call will be in script.py. I tried this giving undefined again it also is not apart of the class. Also could you explain:

math = class1.get_students_with_subject(“Math”)
science = class1.get_students_with_subject(“Science”)

Not seeing why this would be required as math and science are already listed within the roster.py list.

In my version, there wasn’t a math nor science variable in roster.py, so I had to set those up. (I may have an older version of the project.)

There is a get_students_with_subject method for ClassroomOrganizer objects.
I can make a ClassroomOrganizer object, and put it as variable class1 by doing the code below, maybe in script.py:

class1 = classroom_organizer.ClassroomOrganizer()

and at some point after that:

math = class1.get_students_with_subject("Math")
science = class1.get_students_with_subject("Science")

If you already have math and science variables, then you don’t have to do that, I guess.

And after that code, you could have:

and then,

print( math_and_science() )

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.