class Student: def __init__(self, id, fn, ln, dob, m='undefined'): self.id = id self.firstName = fn self.lastName = ln self.dateOfBirth = dob self.Major = m def set_id(self, newid): #This is known as setter self.id = newid def get_id(self): #This is known as a getter return self.id def set_fn(self, newfirstName): self.fn = newfirstName def get_fn(self): return self.fn def set_ln(self, newlastName): self.ln = newlastName def get_ln(self): return self.ln def set_dob(self, newdob): self.dob = newdob def get_dob(self): return self.dob def set_m(self, newMajor): self.m = newMajor def get_m(self): return self.m def print_student_info(self): print(f'{self.id} {self.firstName} {self.lastName} {self.dateOfBirth} {self.Major}') all_students = [] id=100 user_input = int(input("How many students: ")) for x in range(user_input): firstName = input('Enter first name ') lastName = input('Enter last name ') dateOfBirth = input('Enter date of birth ') Major = input('Enter major ') id = id + 1 if Major == '': student = Student(id, firstName, lastName, dateOfBirth, Major) else: student = Student(id, firstName, lastName, dateOfBirth) all_students.append(student) for obj in all_students: obj.print_student_info() search_last_name = input("Enter last name to update major: ") new_major = input("Enter new major: ") found_student = False for obj in all_students: if obj.get_ln() == search_last_name: found_student = True obj.set_major(new_major) print(f"\nMajor updated for {obj.get_first_name()} {obj.get_last_name()} to {new_major}\n") break if not found_student: print("No such student found") print("\nAll students (after major update):") for obj in all_students: obj.print_student_info() Traceback (most recent call last): File "main.py", line 50, in if obj.get_ln() == search_last_name: File "main.py", line 19, in get_ln return self.ln AttributeError: 'Student' object has no attribute 'ln'
class Student:
def __init__(self, id, fn, ln, dob, m='undefined'):
self.id = id
self.firstName = fn
self.lastName = ln
self.dateOfBirth = dob
self.Major = m
def set_id(self, newid): #This is known as setter
self.id = newid
def get_id(self): #This is known as a getter
return self.id
def set_fn(self, newfirstName):
self.fn = newfirstName
def get_fn(self):
return self.fn
def set_ln(self, newlastName):
self.ln = newlastName
def get_ln(self):
return self.ln
def set_dob(self, newdob):
self.dob = newdob
def get_dob(self):
return self.dob
def set_m(self, newMajor):
self.m = newMajor
def get_m(self):
return self.m
def print_student_info(self):
print(f'{self.id} {self.firstName} {self.lastName} {self.dateOfBirth} {self.Major}')
all_students = []
id=100
user_input = int(input("How many students: "))
for x in range(user_input):
firstName = input('Enter first name ')
lastName = input('Enter last name ')
dateOfBirth = input('Enter date of birth ')
Major = input('Enter major ')
id = id + 1
if Major == '':
student = Student(id, firstName, lastName, dateOfBirth, Major)
else:
student = Student(id, firstName, lastName, dateOfBirth)
all_students.append(student)
for obj in all_students:
obj.print_student_info()
search_last_name = input("Enter last name to update major: ")
new_major = input("Enter new major: ")
found_student = False
for obj in all_students:
if obj.get_ln() == search_last_name:
found_student = True
obj.set_major(new_major)
print(f"\nMajor updated for {obj.get_first_name()} {obj.get_last_name()} to {new_major}\n")
break
if not found_student:
print("No such student found")
print("\nAll students (after major update):")
for obj in all_students:
obj.print_student_info()
Traceback (most recent call last): File "main.py", line 50, in if obj.get_ln() == search_last_name: File "main.py", line 19, in get_ln return self.ln AttributeError: 'Student' object has no attribute 'ln'
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 2 images