Не работает валидация Python
Пишу класс на пайтоне и мне нужно сделать валидацию на поля класса решил сделать это сразу в конструкторе при помощи try-except шаманил шаманил, а выбивает ошибку на целочисленных значениях полей класса не понимаю в чем ошибка
class Employee:
def __init__(self):
self.surname = ""
self.year = 0
self.salary = 0
self.company = ""
self.city = ""
self.birth_year = 0
def __init__(self, surname, year, salary, company, city, birth_year):
try:
if surname.isalpha():
self.surname = surname
else:
raise Exception()
except Exception as e:
print("Incorrect surname input!")
try:
int(year)
if year.isdigit() and 0 < year < 100:
self.year = year
else:
raise Exception()
except Exception as e:
print("Incorrect year input!")
try:
int(salary)
if salary.isdigit() and 0 < salary < 100000:
self.salary = salary
else:
raise Exception()
except Exception as e:
print("Incorrect salary input!")
try:
if company.isalpha():
self.company = company
else:
raise Exception()
except Exception as e:
print("Incorrect company's name input!")
try:
if city.isalpha():
self.city = city
else:
raise Exception()
except Exception as e:
print("Incorrect city's name input!")
try:
int(birth_year)
if birth_year.isdigit() and 1950 < birth_year < 2000:
self.birth_year = birth_year
else:
raise Exception()
except Exception as e:
print("Incorrect year of birth input!")
def __str__(self):6
print("{Surname:"+self.surname+";year:"+int(self.year)+";salary:"+int(self.salary)+";company:"+self.company+";city:"+self.city+";birth year:"+int(self.birth_year)+"}")
emp = Employee("Matcha", 20, 2000, "Epam", "Uzhhorod", 1999)
print(str(emp))
вот что выдает компилятор:
Incorrect year input!
Incorrect salary input!
Incorrect year of birth input!
Traceback (most recent call last):
File "C:/Users/dell/PycharmProjects/untitled/Employee.py", line 99, in <module>
print(str(emp))
File "C:/Users/dell/PycharmProjects/untitled/Employee.py", line 95, in __str__
print({Surname:"+self.surname+";year:"+int(self.year)+";salary:"+int(self.salary)+";company:"+self.company+";c ity:"+self.city+";birth year:"+int(self.birth_year)+"}") AttributeError: 'Employee' object has no attribute 'year'