A. Kather
A. Kather

Reputation: 47

Create a course using classes in Python

Here are my instructions, followed by my code. I am having trouble figuring out how to create an empty list to put the students into (a roster) and then of course how to remove students from that list.

The Course class should have the following methods:

  1. __init__ takes and stores a single integer course number.
  2. get_course_number takes no arguments and returns the integer course number.
  3. add_student takes a single string student name and adds it in a roster.
  4. drop_student takes a single string student name. If the student is currently in the roster, it removes that student name from the roster and returns nothing.
  5. get_roster takes no arguments and returns a list containing the names of all students enrolled in the course in alphabetical order.

This is what I have written so far:

class Course(object):

    def __init__(self,course_number):
         self.course_number = int(course_number)

    def get_course_number(self):
         print (self.course_number)

    def add_student(self, student):
        self.list=[]
        self.student= student
        self.list.append(student)

    def drop_student(self, student_id):
        self.student_id= student_id
        if student_id in self.student:
        del self.student[student_id]

    def get_roster(self):
        print (sorted(self.student))

Any insight is much appreciated!

Upvotes: 0

Views: 4025

Answers (3)

Kyle Higginson
Kyle Higginson

Reputation: 952

I think this is what you need, just a few changes:

class Course(object):

    def __init__(self,course_number):
        self.course_number = int(course_number)
        self.students = []

    def get_course_number(self):
         print (self.course_number)

    def add_student(self, student_id):
        self.students.append(student_id)

    def drop_student(self, student_id):
        if student_id in self.students:
            self.students.remove(student_id)

    def get_roster(self):
        print (sorted(self.students))

    def show_students(self):
        print self.students

Here is an example in a shell:

>>> from stack import Course
>>> course = Course(2)
>>> course.add_student(1)
>>> course.add_student(3)
>>> course.show_students()
[1, 3]
>>> course.drop_student(3)
>>> course.show_students()
[1]
>>> course.add_student(5)
>>> course.show_students()
[1, 5]
>>> course.get_roster()
[1, 5]

Upvotes: 0

Dimitris Fasarakis Hilliard
Dimitris Fasarakis Hilliard

Reputation: 160587

You have done a good job so far. The main mistake you've made is in add_student where you have written:

def add_student(self, student):
    self.list=[]
    self.student= student
    self.list.append(student)

here self.list is obviously meant to represent the "roster". Unfortunately, as you can see, every time you invoke add_student you set the "roster" to an empty list.

Instead of creating the roster there, just create an empty roster in your __init__ function:

def __init__(self,course_number):
    self.course_number = int(course_number)
    self.roster = [] 

This list will be used to store students. Now you add_student is straight-forward: just append to the roster:

def add_student(self, student):
    self.roster.append(student)

Another issue to point out. Your requirements state you return the course number and sorted list of students but you actually don't do that, you print them. Instead of using print, use return there (and remember that get_roster should return the roster containing the duplicates).

drop_student should be easy to do now. You get a student name and you simply need to remove it from the roster, for this you can use the remove method on lists.

Upvotes: 0

jfbeltran
jfbeltran

Reputation: 1818

  1. init takes and stores a single integer course number.

Looks good.

  1. get_course_number takes no arguments and returns the integer course number.

Notice that the question says "return" but you're actually "printing" the integer course number. This means if someone was trying to use your function to ask what the course number is, they would see it printed on the screen, but would not be able to assign it to a variable. You probably want something like:

return (self.course_number)
  1. add_student takes a single string student name and adds it in a roster.

This function is currently creating a new list everytime it's run. You should perhaps make sure this line is in your init function:

self.list=[]

There is also no need to set self.student, as this line does:

self.student= student

This is basically defining a specific student to currently be the Course's student even after the line has run. You also never use the self.student again, so it's a wasted line.

Other than that this function looks fine.

  1. drop_student takes a single string student name. If the student is currently in the roster, it removes that student name from the roster and returns nothing.

Again, no need to assign self.student. Here you want to remove the student from the self.list you would initialize in your init function. Two notes here:

  1. Make sure that any code under an if statement is indented. If I fixed only that in the code you wrote it would look like this:

    def drop_student(self, student_id):
        self.student_id= student_id
        if student_id in self.student:
             del self.student[student_id]
    
  2. Look up the "remove" method for python. Here is a good stackoverflow discussion on it.

  1. get_roster takes no arguments and returns a list containing the names of all students enrolled in the course in alphabetical order.

self.student is not a list of students, but if it was, your code would be right. Make sure you use the list of students that you should be initializing in your init function.

Upvotes: 2

Related Questions