Usetutoringspotscode to get 8% OFF on your first order!

  • time icon24/7 online - support@tutoringspots.com
  • phone icon1-316-444-1378 or 44-141-628-6690
  • login iconLogin

python program classes and pickles

We now have remedies for both of these issues. For this program we will create a bunch of student objects, and we’ll have a dictionary that holds a collection of these student objects. That is, the dictionary will represent a course the students are enrolled in. Using a class will allow us to keep both the student data and the methods that operate on that data together. Collecting the class instances into a dictionary and making use of the pickle module will allow us to store and retrieve our student data without having to do any data conversion.

We might have used a list to contain the student objects, but is this case we will use a dictionary. The key associated with the student object will be the student ID. This allows being able to find a given student directly using their ID, without having to iterate through a loop of students to find the one we want.

So, here’s what we need to do. We’ll use a dictionary to be a container for a given course, CIT101. We will populate that dictionary with instances of a student class.

The class we write to represent a particular student needs to have the following 4 (and only 4) instance variables:

a unique id
a name structured as lastname, firstname, for example, “smith, john”
two exam scores representing a midterm and an final
Your class will also need 3 and only 3 methods. The first method needs to be a constructor to create each student so the user can enter the student ID, name, and 2 exam scores. In addition to the constructor, the class will also need the following 2 methods (and no more).

a helper method to calculate the letter grade for the student based on the average of the exam scores. Use the grading scale used for this class.
a method to return the name, two exam scores, and the letter grade for a student.
Your program will allow adding student instances to the CIT101 dictionary so that the dictionary will hold the information for that course.

Each student instance in the dictionary will use the student ID as a key, and the student object instance associated with that ID will be assigned as the key’s value. Assume the dictionary is called CIT101, and the class is called Student, so calling
    Student(id, name, midterm, final)
creates an instance of the student class. If we were hard-coding all this, it might look like the following.

CIT101 = {}        # create the containing dictionary
# add student instance to CIT101 dictionary
CIT101[“123”] = Student(“123”, “smith, john”, 78, 86)

There is a program very similar in structure to this assignment at the end of the first set of lecture notes on classes. It will be the best guide for getting started on this program.

Your program needs to have a 5-option menu that should look very much like the following. Shown is a sample of entering a new student and then displaying all students and their grades.

program 5 menu

For full credit your menu will need the following 5 options. Each option should call a function that performs the proper task.

Option 1 should call a function to display all the student data for the CIT101 course. The function should display the student’s id, name, midterm and final exam scores, and their letter grade. Use formatting to have everything line up in neat columns for option 1. The letter grade should be calculated by the helper method of the student class.
Option 2 should call a function that allows adding a new student instance to the CIT101 dictionary.
Option 3 should call a function that displays the data, including letter grade, for a particular student’s after entering the student ID for that student.
Option 4 allows for a student to drop the class, so here call a function that deletes a student from the dictionary after entering the id of that student.
the last option pickles the dictionary for future processing.
In order to read the class from the pickle file the file needs to exist first. The example given in the lecture notes offers one solution for this issue.

You can leave a response, or trackback from your own site.

Leave a Reply

Powered by WordPress | Designed by: Premium WordPress Themes | Thanks to Themes Gallery, Bromoney and Wordpress Themes