0

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.

Mat
208k41 gold badges409 silver badges423 bronze badges
asked Jul 27, 2012 at 3:06
5
  • 1
    I suspect that this is homework. If so, please tag it as such. Commented Jul 27, 2012 at 3:20
  • 1
    Rather than the code that does work, you should post some minimal examples of what you've tried without success. Commented Jul 27, 2012 at 3:26
  • Your code will throw NameError when run, I suppose. Is this what you get? Commented 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. Commented Jul 27, 2012 at 3:50
  • I do wish that if they teach Python they would teach it properly... not like it was Java. Commented Jul 27, 2012 at 14:12

3 Answers 3

1

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 ;)

answered Jul 27, 2012 at 4:15
Sign up to request clarification or add additional context in comments.

2 Comments

Another part of the assignment may ask for a Course class. And yes, accessors and mutators are largely superfluous in Python, but they do have their uses. Also, if the homework asks for them, I wouldn't try contravening the requirements.
It does ask for a Course class that holds the course name, credit hour, and instructor name. My problem really is that I'm not sure how to achieve what you did using that Course class. Initially, I had something very similar to what you've posted but I'm not sure how to handle it with Course involved.
0

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).

answered Jul 27, 2012 at 4:19

2 Comments

This is a great description and I understand the concept in theory. The key is the identifier while the items following are the values themselves. Though, I'm not sure how to map the coursename to that dictionary located in Student.
When dealing with the dictionary of course->grade inside Student, you can either use Course instances as keys (since in Python, everything is copied by reference unless you copy by value with the 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.
0

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:

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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