1
Meine Arbeit-out: Dies ist eine ZuordnungPython Klasse __str__
class Person(object):
def __init__(self, name):
self.name = name
def __str__(self):
if isinstance(person1, Lecturer):
return "Name: " + self.name + "\tOccupation: " + self.occupation
elif isinstance(person2, Student):
return "Name: " + self.name + "\tStudent Number: " + self.studentNumber
class Lecturer(Person):
def Occupation(self, occupation):
self.occupation = occupation
class Student(Person):
def StudentNumber(self, studentNumber):
self.studentNumber = studentNumber
person1 = Lecturer("Elisha Nsemwa")
person2 = Student("Fabian Hamza")
person1.Occupation("Senior Lecturer")
person2.StudentNumber("HD5603")
print person1
print person2
Meine Ausgabe:
Name: Elisha Nsemwa Occupation: Senior Lecturer
"assignment.py", line 26, in <module>
print person2
"assignment.py", line 7, in __str__
return "Name: " + self.name + "\tOccupation: " + self.occupation
AttributeError: 'Student' object has no attribute 'occupation'
person1 das, wenn ein Teil ausführen, dies OK ist die Ausgabe gedruckt werden, jetzt ist mein Fehler person2 Führe das, wenn nicht das Elif aus; wie kann ich diese