0

Ok, i've gotten pretty far on a code but keep getting an error. I know it's something simple but i just can't put my finger on it please help.

import os
import pickle

class Student(object):

    def _init_(self):
        self._students = {}

    def addStudent(self, name):
        self._students[name] = [0, 0, 0]

    def editStuData(self):
        print()
        print("Edit student data: ")
        stud = input("Enter Student's name: ")
        self._students[stud][0] = int(input("Enter first exam grade: "))
        self._students[stud][1] = int(input("Enter second exam grade: "))
        self._students[stud][2] = int(input("Enter third exam grade: "))

    def dispAverage(self):
        print()
        print("Student Name     Exam 1     Exam 2     Exam 3     Grade")
        for stud in self._students:
            ex1, ex2, ex3 = self._students[stud]

            try:
                avg = (ex1 + ex2 + ex3) / 3
            except ZeroDivisionError:
                avg = 0

            if avg >= 90:
                letterGrade = 'A'
            elif avg >= 80 and avg < 90:
                letterGrade = 'B'                
            elif avg >= 70 and avg < 80:
                letterGrade = 'C'               
            elif avg >= 60 and avg < 70:
                letterGrade = 'D'      
            elif avg < 60:
                letterGrade = 'F'
            print("%-12s %5s %5s %5s %5s" % (name, ex1, ex2, ex3, letterGrade))


def storeFile(fname, c):
    f = open(fname, "wb")
    pickle.dump(c, f)
    f.close()

def readData(fname):
    try:
        f = open(fname, "rb")
        t = pickle.load(f)
        f.close()
        return t

    except IOError as e:
        print(e)

def main():
    fname = "pickledStudents.txt"
    if os.path.exists(fname):
        citStudents = readData(fname)
    else:
        citStudents = Student()

    while True:

        print("""
    Menu Options. Choose 1, 2, 3, or 4

    1. Add a student (scores default to 0, 0, 0)
    2. Edit an exam score
    3. Calculate grades
    4. Save and exit

    """)

        opt = input("ENter your choice, 1, 2, 3, or 4: ")

        if opt == "1":
            print()
            name = input("Enter student name: ")
            citStudents.addStudent(name)

        elif opt == "2":
            citStudents.editStuData()

        elif opt == "3":
            citStudents.dispAverage()

        elif opt == "4":
            storeFile(fname, citStudents)
            print("data stored")
            print()
            break
        else:
            print("Invalid entry, please try again")
            print()

main()
2
Contributors
1
Reply
14
Views
3 Years
Discussion Span
Last Post by BearofNH
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.