Hello Everyone,
I am new to Python(New to Coding too). I have a task to code a function(dayOfYear) where I need to calculate corresponding day of the year. I tried my best to do it and gave up. I have solutions in my material but I want to do it my way. Please help me out. Please find the code below.
I am 100% sure there is no wrong in isYearLeap and daysInMonth functions. I have to do something with dayOfYear(Because I wrote it :P). I need help the way it is.
I am new to Python(New to Coding too). I have a task to code a function(dayOfYear) where I need to calculate corresponding day of the year. I tried my best to do it and gave up. I have solutions in my material but I want to do it my way. Please help me out. Please find the code below.
I am 100% sure there is no wrong in isYearLeap and daysInMonth functions. I have to do something with dayOfYear(Because I wrote it :P). I need help the way it is.
def isYearLeap(year):
if year % 4 != 0:
return False
elif year % 100 != 0:
return True
elif year % 400 != 0:
return False
else:
return True
def daysInMonth(year, month):
if year < 1582 or month < 1 or month > 12:
return None
days = [31,28,31,30,31,30,31,31,30,31,30,31]
result = days[month-1]
if month == 2 and isYearLeap(year):
result = 29
return result
def dayOfYear(year, month, day):
if year < 1582 or month < 1 or month >12 or day < 1 or day >31:
return None
result = 0
while isYearLeap(year)==True:
for i in range(1,13):
susmith = daysInMonth(year,month)
result += susmith
return result
while isYearLeap(year)==False:
for i in range(1,13):
susmith = daysInMonth(year,month)
result += susmith
return result
print(dayOfYear(2000, 12, 31))I am getting output of 31 but not 366
