0

I have a problem with this program.. I'm extremely new to objects and classes... so basically I got a class roster that is going to be ultimately assigned to a course. I'm trying to find out how to exactly put the students and instructors inside of a course. I'm kind of stuck here, i got everything pretty much figured out but, like i said I'm new to this, I can't get the add and remove feature implemented. any help would be greatly appreciated. Thank you in advanced.

class course:
   def __init__(self, courseName, capacity):
     self.courseName = courseName
     self.capacity = capacity


   def add_student(self,key):
     self.student[key] = value

   def remove_student(self,del_key):
     del self.student[del_key]
3
  • 1
    As a side note, in Python 2.x, always inherit from object (unless you have something else to inherit from); otherwise you get "classic classes", which have all kinds of annoying quirks that you don't want to learn about or deal with. Commented Dec 16, 2013 at 21:48
  • As another side note, while your capitalization style is consistent, using the same style for class names and variables like this makes it very easily to create silly bugs later on where you hide or replace a class with a variable. That's why PEP 8 suggests using CamelCase class names, lower_case variables and functions. Commented Dec 16, 2013 at 21:54
  • " I can't get the add and remove feature implemented. any help would be greatly appreciated." Is not a question. What's wrong with what you have. Please also look at sscce.org and whathaveyoutried.com. Commented Dec 16, 2013 at 22:12

1 Answer 1

1

You're pretty close here, but you have two problems.

First, your add_student and remove_student methods are trying to mutate some dictionary named self.student, but you forgot to create it. Do that in the __init__ method, like this:

def __init__(self, courseName, capacity):
    # existing stuff
    self.student = {}

Next, your add_student needs to actually take a value parameter if you want to use it:

def add_student(self, key, value):
    self.student[key] = value

And that's it. Now you can write code like this:

johnsmith = student('John', 'Smith', 14, 3.5)
intropython = course('Python 1', 20)
intropython.add_student('John Smith', johnsmith)

Of course you'll probably want to add more stuff to this later—e.g., a way to get a default key for a student (like his first and last names), checking whether len(self.student) == self.capacity before adding another student, etc. But this should get you started.

Sign up to request clarification or add additional context in comments.

4 Comments

I tried your version of the add_student function and i still get an error. >>> course.add_student(student1) Traceback (most recent call last): File "<pyshell#67>", line 1, in <module> course.add_student(student1) TypeError: add_student() takes exactly 3 arguments (2 given) and when i try it your way... Traceback (most recent call last): File "<pyshell#66>", line 1, in <module> course.add_student('Samuel Sam', student1) File "C:/Users/Andy/Downloads/c&o.py", line 36, in add_student self.student[key] = value AttributeError: course instance has no attribute 'student'
@Aegg: For the second problem, you clearly didn't fix the first part. If you never set self.student = {} anywhere, you will get an AttributeError trying to access or modify self.student, because you're trying to access or modify something that doesn't exist.
@Aegg: Meanwhile, for the first problem, if you declare a function with two parameters, you have to call it with two arguments. More specifically, you need to pass a key and a value if you want to use a key and a value. If you want it to automatically come up with an appropriate key for the value (like, say, the first and last names) instead of taking a parameter, you have to write the code that does that. I explained that in the last paragraph.
@Aegg: More generally, it seems like you didn't actually read the answer, you just tried to copy and paste the code without any attempt at understanding it. That will never get you very far in programming.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.