Python Forum

Full Version: Something is Wrong with my code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.

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
Take a look at this bit:

    while isYearLeap(year)==True:
        for i in range(1,13):
            susmith = daysInMonth(year,month)
            result += susmith
            return result
You have a while loop. Note that this while loop is infinite, because year never changes, so if it's true at the start of the loop, it's always true. Yes, you return. We'll get to that. Now you have a for loop, from 1 to 12, presumably the months. Then you get the number of days in the target month. So every time through the loop, you are going to get the same number of days. You add that to the result, and return the result. Return stops the function from processing. So neither the while loop nor the for loop will execute a second time. That's why you just get the number of days in the target month.

How do you fix this? First get rid of the while statement, and the whole second while loop. Note that the code in both of them is identical. This is because leap years are handled in daysInMonth. You don't need to worry about it here.. The for loop should go from 1 to one less than the target month (range(1, month)). You don't want to do the target month, because you don't finish that month. That's the month the date is in. Then you unindent the return statement, so the for loop can get all of the months before the target month. Finally, you return result + day instead of just result, to account for the number of days in the target month.
Hi ichabod801,

Thank you so much for helping me out.
I understood what you said and implemented changes. Please find the updated code below

def dayOfYear(year, month, day):
    if year < 1582 or month < 1 or month >12 or day < 1 or day >31:
        return None
    result = 0
    for i in range(1,month):
        susmith = daysInMonth(year,month)
        result += susmith
    return result + day
Unfortunately, I got an output of 372 but not 366. Please help me with this last bit.
You are still summing the same month over and over again on line 6. Hint: month doesn't change during the for loop, i does.
Thank you so much. It worked.