0

Need help fixing python program:
I keep getting the following when I try to add students to the list:
line 72, in add_student
entry = students_lab10.Students(name, idn, gpa, grade, hours)
NameError: name 'students_lab10' is not defined

#

Create a class named Student that holds the following data about a student:
1. Name
2. Student ID number
3. GPA
4. Expected grade in this course
5. Full time or part time
Create five student objects from this class and pass data to fill in the class data above.
Besides creating the objects, you will write a menu-driven program that performs the following tasks:
1. Look up and print the student GPA
2. Add a new student to the class
3. Change the GPA of a student
4. Change the expected grade of a student
5. Print the data of all the students in a tabular format
6. Quit the program

#

Inline Code Example Here
class Students:

def __init__(self, name, idn, gpa, grade, hours):

    self.__name = name
    self.__idn = idn
    self.__gpa = gpa
    self.__grade = grade
    self.__hours = hours

def set_name(self, name):
    self.__name = name

def set_idn(self, idn):
    self.__idn = idn

def set_gpa(self, gpa):
    self.__gpa = gpa

def set_grade(self, grade):
    self.__grade = grade

def set_hours(self, hours):
    self.__hours = hours

def get_name(self):
    return self.__name

def get_idn(self):
    return self.__idn

def get_gpa(self):
    return self.__gpa

def get_grade(self):
    return self.__grade

def get_hours(self):
    return self.__hours

def __str__(self):

    return "Name: " + self.__name + \
           "\nIDN: " + self.__idn + \
           "\nGPA: " + self.__gpa + \
           "\ngrade: " + self.__grade + \
           "\nhours: " + self.__hours

Inline Code Example Here

import Students
import pickle

#Global Constants

FIND_GPA = 1
ADD_STUDENT = 2
CHANGE_GPA = 3
CHANGE_GRADE = 4
PRINT_DATA = 5
QUIT = 6

FILENAME = 'students_lab10.dat'

def main():
    mystudents = load_students_lab10()
    choice = 0
    while choice != 6:
        choice = get_menu_choice()
        if choice == 1:
            find_gpa(mystudents)
        elif choice == 2:
            add_student(mystudents)
        elif choice == 3:
            change_gpa(mystudents)
        elif choice == 4:
            change_grade(mystudents)
        elif choice == 5:
            print_data(mystudents)
            save_students(mystudents)

def load_students_lab10():
    try:
        input_file = open(FILENAME, 'rb')
        students_lab10_dct = pickle.load(input_file)
        input_file.close()
    except IOError:
        students_lab10_dct = {}
    return students_lab10_dct

def get_menu_choice():
    print()
    print("Menu")
    print("------------------------------")
    print("1. Look up and print a student's GPA")
    print("2. Add a new student to the class")
    print("3. Change the GPA of a student")
    print("4. Change the expected grade of a student")
    print("5. Print the data of all the students")
    print("6. Quit the program")
    print()
    choice = int(input("Please enter your choice, 1-6: "))
    while choice < 1 or choice > 6:
        choice = int(input("Please enter a valid choice, 1-6: "))
    return choice

def find_gpa(mystudents):
    name = input("Enter a name: ")
    if name in mystudents:
        print(mystudents.gpa) 
    else:    
        print(mystudents.get(name, 'That name is not found.'))   
        print()

def add_student(mystudents):
    name = input("Enter a new student's name: ")
    idn = input("Please enter the student's ID number: ")
    gpa = input("Please enter the student's GPA: ")
    grade = input("Please enter the student's expected grade in this course: ")
    hours = input("Please enter if this student is full or part-time: ")
    print()
    entry = students_lab10.Students(name, idn, gpa, grade, hours)
    if name not in mystudents:
        mystudents[name] = entry
        print("The entry has been added.")
        print()
    else:
        print("That name already exists.")
        print()

def change_gpa(mystudents):
    name = input("Enter a name: ")
    if name in mystudents:
        gpa = input("Please enter the student's new GPA: ")
        entry = students_lab10.Students(name, idn, gpa, grade, hours)
        mystudents[name] = entry
        print("GPA updated.")
        print()
    else:
        print("That name is not found.")

def change_grade(mystudents):
    name = input("Enter a name: ")
    if name in mystudents:
        grade = input("Please enter the student's new grade: ")
        entry = students_lab10.Students(name, idn, gpa, grade, hours)
        mystudents[name] = entry
        print("Grade updated.")
        print()        
    else:
        print("That name is not found.")
        print()

def print_data(mystudents):
    input_file = open(FILENAME, 'rb')
    print(mystudents)    
    print()
    input_file.close()

def save_students(mystudents):
    output_file = open(FILENAME, 'wb')
    pickle.dump(mystudents, output_file)
    output_file.close()

main()
2
Contributors
3
Replies
12
Views
1 Year
Discussion Span
Last Post by hericles
1

What is students_lab10? As the error states, it isn't defined in your code. The first time it appears is when you are trying to use it.

Votes + Comments
Thank you, now I am getting other errors when selecting 1 in the menu
0
`Inline Code Example Here`
I changed students_lab10 to students.Students

I am able to load names and data but then when I select 1 from the menu I get the following:
line 60, in find_gpa
    print(mystudents.gpa)

AttributeError: 'dict' object has no attribute 'gpa'

0

in that case you'll need to check that
a) the file you're reading has that attribute in it
b) that attribute is getting read correctly from the file
c) it exists in the mystudents object

That error is happening because the structure 'mystudents' doesn't have .gpa ( you probably guessed that much).

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.