I was asked, as an exercise, to build a class in python, named "Date". I have to build few functions according to the requirements. The first one is an "init" model that takes a day, month, year, hours, and minutes and returns a valid Date object(when valid means that a month, for instance, can't be 13.) The next is a function that raises a specific error if some Data is invalid, like 13 as a month. My problem is that Whenever I call "Date(23,14,1998).month", it returns "13" instead of an error. I am also not sure how a valid Date object is created. I could use your help. My code:
class Date:
days_per_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
def __init__(self, day, month, year, hours=0, minutes=0):
self.day=day
self.month=month
self.year=year
self.hours=hours
self.minutes=minutes
def validate(self):
if type(self.month)==int:
if self.month<1:
raise ValueError('Error: month value must be positive')
if self.month>12:
raise ValueError('Error: month value cannot exceed 12')
else:
raise ValueError('Error: month value must be an integer')
if type(self.day)==int:
if self.day>days_in_month(self):
raise ValueError('Error: maximum days in this month exceeded')
elif self.day<1:
raise ValueError('Error: day value must be positive')
else:
raise ValueError('Error: day value must be an integer')
if type(self.hours)==int:
if self.month>23:
raise ValueError('Error: hours value cannot exceed 23')
elif self.month<0:
raise ValueError('Error: hours value cannot be negative')
else:
raise ValueError('Error: hours value must be an integer')
if type(self.minutes)==int:
if self.month>59:
raise ValueError('Error: minutes value cannot exceed 59')
elif self.month<0:
raise ValueError('Error: minutes value cannot be negative')
else:
raise ValueError('Error: minutes value must be an integer')
def days_in_month(self):
if self.month == 2 and self.is_leap_year():
return 29
return self.days_per_month[self.month-1]
def is_leap_year(self):
if self.year % 400 == 0:
return True
elif self.year % 100 == 0:
return False
elif self.year % 4 == 0:
return True
else:
return False
def __init__()really not indented?