I'm working on a project here and am pretty confused as of how to handle this next part. Included is the project description.
Student class: The student class will store the information about the student id, student’s first and last names, and a dictionary named grades that contains the course number and letter grades of the classes the student has taken. You will write accessor methods for student id, first name, and last name and mutator methods for student’s first name and last name. There will be two more methods in the Student class as shown below.
- getCourseNumbers(): it returns a list of course numbers that the student has taken. It returns an empty list if the student has not taken any courses.
- getGrade(course_no): it returns the grade the student has received in the course identified by the parameter course_no. If the given course number is not found, this function returns ‘Z’ to indicate that.
- addCourse(course_no, grade): adds a course with the course number and grade
- updateCourse(course_no, grade): updates an existing course’s grade
- deleteCourse(course_no): deletes the course from the dictionary.
Here is my code thus far:
class Course:
def __init__ (self, __crn, __ch, __ins):
self.__crn = crn
self.__ch = ch
self.__ins = ins
def coursename(self):
return self.__crn
def credithour(self):
return self.__ch
def instructor(self):
return self.__ins
class Student:
def __init__(self, id, sfirst, slast):
self.sid = sid
self.sfirst = sfirst
self.slast = slast
def studentid(self):
return self.sid
def studentfirst(self):
return self.sfirst
def studentlast(self):
return self.slast
def main():
course = Course('CSC 1100', '4', 'Name')
print (course.coursename())
print (course.credithour())
print (course.instructor())
main()
So, my question really is. Now that I've made my initial two classes, I'd like to create a dictionary that contains the coursename and the students grade. What's the best way to do this? I'm kind of at a loss and have tried many different ways with no success.
-
1I suspect that this is homework. If so, please tag it as such.IT Ninja– IT Ninja2012年07月27日 03:20:28 +00:00Commented Jul 27, 2012 at 3:20
-
1Rather than the code that does work, you should post some minimal examples of what you've tried without success.Isaac– Isaac2012年07月27日 03:26:44 +00:00Commented Jul 27, 2012 at 3:26
-
Your code will throw NameError when run, I suppose. Is this what you get?Tadeck– Tadeck2012年07月27日 03:45:44 +00:00Commented Jul 27, 2012 at 3:45
-
The code runs and outputs correctly. The problem I'm having is implementing the dictionary that is going to take in the course name and letter grade. Before, I was trying to bring in the dictionary information by using dictionary = {Course.coursename():var} where var is a users input - Though, after realizing that the Course class had to have private members, that way wouldn't work.user1556509– user15565092012年07月27日 03:50:36 +00:00Commented Jul 27, 2012 at 3:50
-
I do wish that if they teach Python they would teach it properly... not like it was Java.Chris Morgan– Chris Morgan2012年07月27日 14:12:25 +00:00Commented Jul 27, 2012 at 14:12
3 Answers 3
Why do you have a Course class? Nothing in the question asks for one; in fact, it specifies that you are to store the information as a dictionary.
I don't agree with the request for accessor methods; that is a Java idiom, not Pythonic. Similarly, the given method names are javaCase, contrary to PEP8.
The code then reduces to
class Student(object):
def __init__(self, id, firstname, lastname, grades=None):
self.id = id
self.firstname = firstname
self.lastname = lastname
self.grades = {} if grades is None else dict(grades)
def get_course_numbers(self):
return self.grades.keys()
def get_grade(self, course):
return self.grades.get(course, 'Z')
def add_course(self, course, grade):
self.grades[course] = grade
... I'll leave the last couple of methods as an exercise ;)
2 Comments
Recall that a dictionary is just a key-value mapping - it associates keys with particular values. So what you're being asked for is just storing with each student a mapping from a unique identifier for a course to a letter grade (stored e.g. as a string). A dictionary is an excellent tool for this task.
Recall that you can use class instances as keys, even if instances of that class are mutable. The reason is that it's not really the instance itself being used as a key, but its unique identifier (memory address).
It would probably be more apt, though, to have a way to map course numbers to Course instances. Notice the keyword map there? You could also store all the courses in a list and do a search every time you wanted to find a course by number, but that would be slow (for len(courses) -> Infinity).
2 Comments
copy module), or you can use course-number strings as keys along with a separate, global dictionary to map course-number strings to Course instances. These are just ideas; since this is homework, the actual method you choose is completely up to you.Dictionary
DIFFICULT - Create a dictionary where the keys are course codes and the values are course names. Allow the user to:
Add a new course
Update an existing course name
Delete a course
Display all courses
EASY - Given the dictionary: