Question: Hello, I need help fixing my code as follows: -Be sure to alphabetize your methods per the guidance given in the lecture. -On lines 68,
Hello, I need help fixing my code as follows:
-Be sure to alphabetize your methods per the guidance given in the lecture.
-On lines 68, 84, 100, and 108, you are calling the class directly instead of using self. Use self - your code will be more reliable that way.
-Your getLastInstitution method needed to work more like the getDegree code. You have too much going on in the getLastInstitution method.
-The isNameOK and isEmailOK functions needed to be methods on the Validator class.
-You have a little redundant code in your main program loop. Get the name and email address and then split off to gathering student\instructor specific information. You otherwise did a nice job there.
-If you enter an invalid value when you're asked to enter user type, your program crashes.
Code:
# base class
class Individual:
def __init__(self,userId, name, emailAddress):
self.userId = userId
self.name = name
self.emailAddress = emailAddress
def displayInformation(self):
print("ID: " + str(self.userId))
print("Name: " + str(self.userId))
print("Email: " + str(self.userId))
# child class Instructor inherits from Individual class
class Instructor(Individual):
def __init__(self, userId, name, emailAddress, institution, highestDegree):
super().__init__(userId, name, emailAddress)
self.institution = institution
self.highestDegree = highestDegree
def displayInformation(self):
print("Instructor ")
super().displayInformation()
print("Institution: " + str(self.userId))
print("Highest Degree: " + str(self.userId))
# child class Student inherits from Individual class
class Student(Individual):
def __init__(self,userId, name, emailAddress, program):
super().__init__(userId, name, emailAddress)
self.program = program
def displayInformation(self):
print("Student")
super().displayInformation()
print("Program of Study:" + str(self.program))
#validator class to validate user input
class Validator:
def validateId(userId, passed_max_length):
if len(userId) <= passed_max_length and userId.isdigit():
return True
else:
print("This is inputted improperly. Please try again")
return False
def validateUser(answer):
if answer:
return True
else:
print("Input is missing. Please try again.")
return False
#Validating email
def getEmail(self):
email_ok = False
while not email_ok:
user_email = input("Enter User email: ")
email_ok = isEmailOK(user_email)
return user_email
#method to get student id from user
def getStudentId(self):
userInput = False
while not userInput:
userId = input("Enter User Id: ")
userInput = Validator.validateId(userId, 7)
return userId
#metod to get individual name
def getName(self):
name_ok = False
while not name_ok:
user_name = input("Enter User name: ")
name_ok = isNameOK(user_name)
return user_name
#method to get instructor id from user
def getinstructorID(self):
userInput = False
while not userInput:
userId = input("Enter User Id: ")
userInput = Validator.validateId(userId, 5)
return userId
#method to get last institution name from user
def getLastInstitution(self):
institution_OK = False
while not institution_OK:
last_institution = input("Enter last institution: ")
if last_institution: #make all while loop like this
institution_OK = True
#method to get highet degree name from user
def getDegree(self):
userInput = False
while not userInput:
highestDegree = input("Enter highest degree: ")
userInput = Validator.validateUser(highestDegree)
return highestDegree
#method to get program name from user
def getProgram(self):
userInput = False
while not userInput:
program = input("Enter program name: ")
userInput = Validator.validateUser(program)
return program
#validating email
def isNameOK(passed_user_name):
bad_chars = ["!", '"', "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "=", "+", ",", "<", ">", "/", "?", ";", ":", "[", "]", "{", "}", "\\", "."]
if passed_user_name:
for character in passed_user_name:
if character in bad_chars or character.isdigit():
print("User name is incorrect. Please try again.")
return False
return True
else:
print("User name is missing. Please try again.")
return False
#Validating email
def isEmailOK(passed_user_email):
bad_chars = ["!", '"', "#", "$", "%", "^", "&", "*", "(", ")", "_", "=", "+", ",", "<", ">", "/", "?", ";", ":", "[", "]", "{", "}", "\\"]
if passed_user_email:
for character in passed_user_email:
if character in bad_chars or character.isdigit():
print("User email is incorrect. Please try again.")
return False
return True
else:
print("User email is missing. Please try again.")
return False
my_validator = Validator()
#list to store the records
college_records = []
print("Store college records")
#while loop
while True:
user = int(input("Please select type of user: 1 for instructor, 2 for student, 3 exit/print "))
#if user is instructor
if user == 1:
temp_instructor_ID = my_validator.getinstructorID()
temp_name = my_validator.getName()
temp_emailAddress = my_validator.getEmail()
temp_institution = my_validator.getLastInstitution()
temp_degree = my_validator.getDegree()
instructor_instance = Instructor(temp_instructor_ID, temp_name, temp_emailAddress, temp_institution, temp_degree)
college_records.append(instructor_instance)
#if user is student
elif user == 2:
temp_student_userId = my_validator.getStudentId()
temp_student_name = my_validator.getName()
temp_student_emailAddress = my_validator.getEmail()
temp_student_program = my_validator.getProgram()
Student_instance = Student(temp_student_userId, temp_student_name, temp_student_emailAddress, temp_student_program)
college_records.append(Student_instance)
else:
break
#loop through your college_records list and get everything printed out.
for individual in college_records:
individual.displayInformation()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
