Stack Overflow Asked by Katie Schmidt on January 3, 2021
I’m not understanding why my getters and setters work in the first two classes of my code, but doesn’t on the third class.
I looked at similar questions but could find one that helped me, sorry if there is something and I missed it. I’m still somewhat new to Python so I realize it’s likely something I’m just missing or not aware of yet.
My code:
class Person():
intTotalPeople = int(0)
def __init__(self, firstName, lastName, gender, age):
'Obtain name, gender, gender, and age'
self.firstName = firstName
self.lastName = lastName
self.gender = gender
self.age = age
@property
def firstName(self):
return self.__firstName
@property
def lastName(self):
return self.__lastName
@property
def gender(self):
return self.__gender
@property
def age(self):
return self.__age
@firstName.setter
def firstName(self, firstName):
if firstName != str(firstName):
raise Exception('firstName should contain letters only. The value entered was: {}'.format(firstName))
else:
self.__firstName = firstName
@lastName.setter
def lastName(self, lastName):
if lastName != str(lastName):
raise Exception('lastName should contain letters only. The value entered was: {}'.format(lastName))
else:
self.__lastName = lastName
@age.setter
def age(self, age):
if age == int(age):
self.__age = age
elif age == float(age):
self.__age = age
else:
raise Exception('age should contain numbers only. The value entered was: {}'.format(age))
@gender.setter
def gender(self, gender):
if gender == "Female":
self.__gender = gender
elif gender == "Male":
self.__gender = gender
else:
raise Exception('gender must be either Male or Female only. The value entered was: {}'.format(gender))
class Student(Person):
'Class for data pertaining to current students'
intTotalFemale = int(0)
intTotalMale = int(0)
dblTotalGPA = float(0)
dblAverageGPA = float(0)
intTotalFemaleAge = int(0)
intTotalMaleAge = int(0)
dblAverageFemaleAge = float(0)
dblAverageMaleAge = float(0)
def __init__(self, firstName, lastName, gender, age, GPA):
Person.__init__(self, firstName, lastName, gender, age)
self.GPA = GPA
Person.intTotalPeople += 1
Student.dblTotalGPA += self.GPA
if self.gender == "Female":
Student.intTotalFemale += 1
Student.intTotalFemaleAge += self.age
else:
Student.intTotalMale += 1
Student.intTotalMaleAge += self.age
@property
def GPA(self):
return self.__GPA
@GPA.setter
def GPA(self, GPA):
if GPA < 0:
raise Exception('GPA must be zero or greater. The value you entered was: {}'.format(GPA))
elif GPA > 4.0:
raise Exception('GPA must be 4.0 or less. The value you entered was: {}'.format(GPA))
else:
self.__GPA = GPA
def CalcAverageGPA(self):
Student.dblAverageGPA = Student.dblTotalGPA / Person.intTotalPeople
def CalcAverageAgeByGender(self):
if self.gender == "Female":
Student.dblAverageFemaleAge = Student.intTotalFemaleAge / Student.intTotalFemale
else:
Student.dblAverageMaleAge = Student.intTotalMaleAge / Student.intTotalMale
def __str__(self):
return "nAverage GPA: " + str(Student.dblAverageGPA) + "nAverage Age of Male Grads: " + str(
Student.dblAverageMaleAge) + "nAverage Age of Female Grads: " + str(Student.dblAverageFemaleAge)
class Graduate(Student):
intFemaleJobs = int(0)
intMaleJobs = int(0)
intTotalGrads = int(0)
def __init__(self, firstName, lastName, gender, age, GPA, gradYear, jobStatus):
Student.__init__(self, firstName, lastName, gender, age, GPA)
self.gradYear = gradYear
self.jobStatus = jobStatus
Graduate.intTotalGrads += 1
if self.gender == "Female":
if self.jobStatus == "Y":
Graduate.intFemaleJobs += 1
else:
if self.jobStatus == "Y":
Graduate.intMaleJobs += 1
@property
def GradYear(self):
return self.__gradYear
@property
def JobStatus(self):
return self.__jobStatus
@GradYear.setter
def GradYear(self, gradYear):
if gradYear == int(gradYear):
self.__gradYear = gradYear
else:
raise Exception('gradYear must be in YYYY format. The value you entered was: {}'.format(gradYear))
@JobStatus.setter
def JobStatus(self, jobStatus):
if jobStatus != "Y":
raise Exception('jobStatus must be Y or N. The value you entered was: {}'.format(jobStatus))
elif jobStatus != "N":
raise Exception('jobStatus must be Y or N. The value you entered was: {}'.format(jobStatus))
else:
self.__jobStatus = jobStatus
def __str__(self):
return "Total Graduate Students: " + str(self.intTotalGrads) + Student.__str__(
self) + "nTotal Male Graduates with Jobs: " + str(
Graduate.intMaleJobs) + "nTotal Female Graduates with Jobs: " + str(Graduate.intFemaleJobs)
# import StudentClass file to access Student class
import StudentClass
# ---------------------------------------------------
# MAIN - Call Graduate class and include first name,
# last name, gender, GPA, age, grad year,
# and job status in argument
# and display totals and averages
# ---------------------------------------------------
objStudent1 = StudentClass.Graduate("Katie", "Schmidt", "Female", 29, 4.00, 2009, "Y")
objStudent2 = StudentClass.Graduate("Tammy", "Schmidt", "Female", 60, 3.75, 1978, "Y")
objStudent3 = StudentClass.Graduate("Fred", "Schmidt", "Male", 60, 3.25, 1978, "N")
objStudent4 = StudentClass.Graduate("Emma", "Schmidt", "Female", 35, 2.00, 2002, 2)
objStudent5 = StudentClass.Graduate("Richard", "Evans", "Male", 55, 2.10, 1985, "Y")
StudentClass.Student.CalcAverageGPA(objStudent5)
StudentClass.Student.CalcAverageAgeByGender(objStudent1)
StudentClass.Student.CalcAverageAgeByGender(objStudent5)
print(objStudent5)
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP