OOP and files handling python programming project
belongs to collection: python projects for beginners
All Answers
need an explanation for this answer? contact us directly to get an explanation for this answer
class Student :
def __init__(self,ID,Firstname,Familyname,absence,Exam1,Exam2) :
"""initializes the student; Add new Student"""
self._ID = ID
self._FirstName = Firstname
self._FamilyName = Familyname
self._absences = absence
self._Exam1= Exam1
self._Exam2= Exam2
self._TotalGrade= int(Exam1)+int(Exam2)
def getID(self):
return self._ID
def get_name(self):
""" returns the student name"""
return self._FirstName+' '+self._FamilyName
def get_absence(self):
return self._absences
def get_exam1(self):
"""get exam1 grade """
return self._Exam1
def get_exam2(self):
"""get exam2 grade """
return self._Exam2
def set_ID(self,ID):
self._ID=ID
def set_name(self,Name):
""" set the student name"""
self._FirstName ,self._FamilyName = Name.split()
def set_absence(self,absence):
self._absences=absence
def set_exam1(self,Exam1):
"""set exam1 grade """
self._Exam1 = Exam1
def set_exam2(self,Exam2):
"""get exam2 grade """
self._Exam2 = Exam2
def calculate_total(self):
"""get exam2 grade """
return self._Exam1+self._Exam2
def PrintStudent(self):
print(self._FirstName+', '+self._FamilyName+', '+self._absences+', '+self._Exam1+', '+self._Exam2+', '+str(self._TotalGrade)+'\n')
class Class :
def __init__(self):
"""Sets up a class to hold and update students"""
self.students = []
def addStudent(self,ID,Firstname,Familyname,absence,Exam1,Exam2):
student = Student(ID,Firstname,Familyname,absence,Exam1,Exam2)
self.students.append(student)
def delStudent(self,student):
self.students.remove(student)
def getStudentByID(self,ID):
for student in self.students:
if student._ID == ID:
return student
def getStudentByName(self,FirstName):
StudentsByName = []
for student in self.students:
if student._FirstName == FirstName:
StudentsByName.append(student)
return StudentsByName
def modifyStudentID(self,Student,newID):
Student.set_ID(newID)
return Student
def modifyStudentName(self,Student,newName):
Student.set_name(newName)
return Student
def modifyStudentAbsence(self,Student,newAbsence):
Student.set_absence(newAbsence)
return Student
def modifyStudentExam1(self,student,newExam1):
student.set_exam1(newExam1)
return student
def modifyStudentExam2(self,student,newExam2):
student.set_exam2(newID)
return student
def modifyStudent(self,student):
EditedStudent = Student(student._ID,student._Firstname,student._Familyname,student._absence,student._Exam1,student._Exam2)
delStudent(student)
addStudent(EditedStudent)
def ShowByID(self):
return sorted(self.students, key= lambda e:e._ID)
def ShowByName(self):
return sorted(self.students, key= lambda e:e._FirstName)
def ShowByAbsences(self):
return sorted(self.students, key= lambda e:e._absences, reverse=True)
def ShowByTotal(self):
return sorted(self.students, key= lambda e:e._TotalGrade, reverse=True)
def GetStudentAbsences(self):
AbsentStudent= self.students[0]
for student in self.students:
if(student._absences>AbsentStudent._absences):
AbsentStudent=student
return AbsentStudent
def read_data(filename):
c = Class()
with open(filename, "r") as ClassStudents:
for line in ClassStudents:
ID,Firstname,Familyname,absence,Exam1,Exam2,total = line.strip().split(',')
c.addStudent(ID,Firstname,Familyname,absence,Exam1,Exam2)
return c
def Save_Quit(Students,filename):
f = open(filename, "w")
for student in Students:
line=student._ID+','+student._FirstName+','+student._FamilyName+','+student._absences+','+student._Exam1+','+student._Exam2+','+str(student._TotalGrade)+'\n'
f.write(line)
print('Students data Saved!')
f.close()
def TestAttribute(Att,type):
if Att == '':
return 'You cannot skip this attribute!\n'
if type == 'int':
if not Att.isnumeric():
return 'Enter only numeric values Please!'
elif type == 'str':
if not Att.replace(" ", "").isalpha():
if "'" not in Att:
return 'Enter only Alphabetic values Please!'
def add_new_student(c):
ID = input('Enter Student ID:')
while TestAttribute(ID,'int'):
print(TestAttribute(ID, 'int'))
ID = input('Enter Student ID:')
firstname = input('Enter Student Firstname:')
while TestAttribute(firstname,'str'):
print(TestAttribute(firstname, 'str'))
firstname = input('Enter Student Firstname:')
familyname = input('Enter Student Familyname:')
while TestAttribute(familyname,'str'):
print(TestAttribute(familyname, 'str'))
familyname = input('Enter Student Familyname:')
Absences = input('Enter Student Absences days:')
while TestAttribute(Absences,'int'):
print(TestAttribute(Absences, 'int'))
Absences = input('Enter Student Absences days:')
Exam1 = input('Enter Student Exam1 grade:')
while TestAttribute(Exam1,'int'):
print(TestAttribute(Exam1, 'str'))
Exam1 = input('Enter Student Exam1 grade:')
Exam2 = input('Enter Student Exam2 grade:')
while TestAttribute(Exam2,'int'):
print(TestAttribute(Exam2, 'int'))
Exam2 = input('Enter Student Exam2 grade:')
c.addStudent(ID,firstname,familyname,Absences,Exam1,Exam2)
print('Student added !')
def main_menu(c,filename):
print('Main Menu'.center(20))
fn_no = input('''1- Adding Students to the class
2- Removing students from the class
3- Modifying student information
4- Show all students
5- Show top 10 students
6- Show students with largest absences
7- Terminate a program
''')
if int(fn_no) in range(1, 8):
if fn_no == '1':
add_new_student(c)
main_menu(c,filename)
elif fn_no=='2':
del_by = input('''a. By ID
b. By Student Name
''')
if del_by == 'a':
ID=input('Enter ID of the Student you wish to remove,')
student=c.getStudentByID(ID)
c.delStudent(student)
print("Student removed!")
main_menu(c,filename)
elif del_by == 'b':
Name=input('Enter FirstName of the Student you wish to remove,')
students=c.getStudentByName(Name)
if students:
print("Records found under name ("+Name+')\n')
else:
print("No student with this name found!")
main_menu(c,filename)
for idx,student in enumerate(students):
print(str(idx) +': ')
student.PrintStudent()
index=input('Please select which record to remove:')
c.delStudent(students[int(index)])
print("Student removed!")
main_menu(c,filename)
else:
print("input not recognised. please try again...")
main_menu(c,filename)
elif fn_no == '3':
ID = input('a. Retrieve information by ID:')
student=c.getStudentByID(ID)
print("Choose what to edit ")
select = input('''a. ID
b. NAME
c. Absences
d. Exam1
e. Exam2
''')
if select == 'a':
newID = input('Please enter the new ID:')
while TestAttribute(newID,'int'):
print(TestAttribute(newID, 'int'))
newID = input('Please enter the new ID:')
c.modifyStudentID(student,newID)
print("Student Edited!")
main_menu(c,filename)
elif select == 'b':
newname = input('Please enter the new Name:')
while TestAttribute(newname,'str'):
print(TestAttribute(newname, 'str'))
newname = input('Please enter the new Name:')
c.modifyStudentName(student,newname)
print("Student Edited!")
main_menu(c,filename)
elif select == 'c':
newAbesences = input('Please enter the new Absence days:')
while TestAttribute(newAbesences,'int'):
print(TestAttribute(newAbesences, 'int'))
newAbesences = input('Please enter the new Absence days:')
c.modifyStudentAbsence(student,newAbesences)
print("Student Edited!")
main_menu(c,filename)
elif select == 'd':
newExam1 = input('Please enter the new grade for Exam1:')
while TestAttribute(newExam1,'int'):
print(TestAttribute(newExam1, 'int'))
newExam1 = input('Please enter the new grade for Exam1:')
c.modifyStudentExam1(student,newExam1)
print("Student Edited!")
main_menu(c,filename)
elif select == 'e':
newExam2 = input('Please enter the new Grade for Exam2:')
while TestAttribute(newExam2,'int'):
print(TestAttribute(newExam2, 'int'))
newExam2 = input('Please enter the new Grade for Exam2:')
c.modifyStudentExam2(student,newExam2)
print("Student Edited!")
main_menu(c,filename)
else:
print("input not recognised. please try again...")
main_menu(c,filename)
elif fn_no == '4':
select = input('''a. Sort by ID
b. Sort by Name
c. Sort by Total Marks
d. Sort by Absences
''')
if select == 'a':
students=c.ShowByID()
for student in students:
print(student._ID+': ')
student.PrintStudent()
main_menu(c,filename)
elif select == 'b':
students=c.ShowByName()
for student in students:
print(student._ID+': ')
student.PrintStudent()
main_menu(c,filename)
elif select == 'c':
students=c.ShowByTotal()
for student in students:
print(student._ID+': ')
student.PrintStudent()
main_menu(c,filename)
elif select == 'd':
students=c.ShowByAbsences()
for student in students:
print(student._ID+': ')
student.PrintStudent()
main_menu(c,filename)
else:
print("input not recognised. please try again...")
main_menu(c,filename)
elif fn_no == '5':
students=c.ShowByTotal()
print("The top 10 Students: \n")
for student in students[:9]:
print(student._ID+': ')
student.PrintStudent()
main_menu(c,filename)
elif fn_no == '6':
print("The Student who has the largest number of absences in the class:\n")
student=c.GetStudentAbsences()
print(student._ID+': ')
student.PrintStudent()
main_menu(c,filename)
elif fn_no == '7':
Save_Quit(c.students,filename)
else:
print("input not recognised. please try again...")
main_menu(c,filename)
filename='students.txt'
c=read_data(filename)
main_menu(c,filename)
need an explanation for this answer? contact us directly to get an explanation for this answer
total answers (2)
Main Menu
1- Adding Students to the class
2- Removing students from the class
3- Modifying student information
4- Show all students
5- Show top 10 students
6- Show students with largest absences
7- Terminate a program
1
Enter Student ID:1111
Enter Student Firstname:hesham
Enter Student Familyname:ali
Enter Student Absences days:2
Enter Student Exam1 grade:70
Enter Student Exam2 grade:30
Student added !
Main Menu
1- Adding Students to the class
2- Removing students from the class
3- Modifying student information
4- Show all students
5- Show top 10 students
6- Show students with largest absences
7- Terminate a program