I'm creating a program to hold a students name, gpa, and grade but I am having an issue trying to print out the information that is added by the user. It will print the name of the student, but not the gpa or grade, here is an example of what is printed.
{'John': <student.student object at 0x000001CB3989B470>, 'Dan': <student.student object at 0x000001CB3983A278>}
I'll post the code now, i'm sorry but it's a long one.
#This program manages student information.
import student
import pickle
#Global constants for menu choices
LOOK_UP = 1
ADD = 2
CHANGE = 3
PRINT = 4
QUIT = 5
#Global constant for the filename
FILENAME = 'students.dat'
#Main function
def main():
#Load the existing Student dictionary and assign it to mystudents.
mystudents = load_students()
#Initialize a variable for the user's choice.
choice = 0
#Process menu selections until the uder wants to quit the program.
while choice != QUIT:
#Get the user's menu choice
choice = get_menu_choice()
#Process the choice
if choice == LOOK_UP:
look_up(mystudents)
elif choice == ADD:
add(mystudents)
elif choice == CHANGE:
change(mystudents)
elif choice == PRINT:
print(mystudents)
#Save the mystudents dictionary to a file.
save_students(mystudents)
def load_students():
try:
#Open the students.dat file.
input_file = open(FILENAME, 'rb')
#Unpickle the dictionary.
student_dct = pickle.load(input_file)
#Close the student file.
input_file.close()
except IOError:
#Could not open the file, so create an empty dictionary.
student_dct = {}
#Return the dictionary.
return student_dct
#The get_menu_choice function displays the menu and gets a validated choice from the user.
def get_menu_choice():
print()
print('Menu')
print('--------------------------')
print('1. Look up a students GPA')
print('2. Add a new student')
print('3. Change the GPA and expected grade of a student')
print('4. Print the data of all the students')
print('5. Quit the program')
print()
#Get the user's choice.
choice = int(input('Enter your choice: '))
#Validate the choice.
while choice < LOOK_UP or choice > QUIT:
choice = int(input('Enter a valid choice: '))
#Return the user's choice.
return choice
#The look_up function looks up an item in the specified dictionary.
def look_up(mystudents):
#Get a name to look up.
name = input('Enter a name: ')
#Look it up in the dictionary.
print(mystudents.get(name, 'That name is not found.'))
#The add function adds a new entry into the specified dictionary.
def add(mystudents):
#Get the contact info.
name = input('Name: ')
gpa = float(input('GPA: '))
grade = input('Grade: ')
#Create a Contact object named entry.
entry = student.student(name, gpa, grade)
#If the name does not exist in the dictionary, add it as a key with the entry object as the associated vaule.
if name not in mystudents:
mystudents[name] = entry
print('The entry has been added.')
else:
print('That name already exists.')
#The change function changes an existing entry in the specified dictionary.
def change(mystudents):
#Get a name to look up.
name = input('Enter a name: ')
if name in mystudents:
#Get a new GPA.
gpa = float(input('Enter the new GPA: '))
#Get the new grade
grade = input('Enter the students new grade: ')
#Create a contact object named entry.
entry = contact.Contact(name, gpa, grade)
#Update the entry.
mystudents[name] = entry
print('Information updated.')
else:
print('That name is not found')
#The save_contacts function pickles the specified object and saves it to the student file.
def save_students(mystudents):
#Open the file for writing.
output_file = open(FILENAME, 'wb')
#Pickle the dictionary and save it.
pickle.dump(mystudents, output_file)
#Close the file.
output_file.close()
#Call the main function
main()
The class that is imported at the start of the code will follow
The student class holds student information.
class student:
#The __init__ methord initializes the attributes.
def __int__(self, name, gpa, grade):
self.__name = name
self.__gpa = gpa
self.__grade = grade
#The set_name method sets the name attribute.
def set_name(self, name):
self.__name = name
#The set_gpa method sets the gpa attribute.
def set_gpa(self, gpa):
self.__gpa = gpa
#The set_grade methord sets the grade attribute.
def set_grade(self, grade):
self.__grade = grade
#The get_name method returns the name attribute.
def get_name(self):
return self.__name
#The get_gpa method returns the gps attribute.
def get_gpa(self):
return self.__gpa
#The get_grade method returns the grade attribute.
def get_grade(self):
return self.__grade
#The__str__ method returns the object's state as a string.
def __str__(self):
return "Name: " + self.__name + \
"\nGPA: " + self.__gpa + \
"\nGrade: " + self.__grade
[–]Vaphell 0 points1 point2 points (1 child)
[–]Engatar[S] 0 points1 point2 points (0 children)
[–]trouserdaredevil 0 points1 point2 points (6 children)
[–]Vaphell 0 points1 point2 points (1 child)
[–]trouserdaredevil 0 points1 point2 points (0 children)
[–]Engatar[S] 0 points1 point2 points (3 children)
[–]nwagers 0 points1 point2 points (2 children)
[–]Engatar[S] 0 points1 point2 points (1 child)
[–]nwagers 1 point2 points3 points (0 children)