Python Forum
Something is Wrong with my code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Something is Wrong with my code
#1
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
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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.
Reply
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Thank you so much. It worked.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  im not sure what ive done wrong code doesnt run dgizzly 3 2,218 Nov-16-2022, 03:02 AM
Last Post: deanhystad
  what is wrong with my code 53535 4 2,403 Apr-07-2022, 11:37 AM
Last Post: 53535
  What's wrong with my code? NeedHelpPython 4 3,080 Oct-22-2021, 07:59 PM
Last Post: Yoriz
  Help with my code due 11:59 pm, can you tell me where I went wrong and help fix it? shirleylam852 1 3,366 Dec-09-2020, 06:37 AM
Last Post: stranac
  I am getting an incorrect average, and not sure why? What's wrong with my code? shirleylam852 8 6,493 Nov-20-2020, 05:32 AM
Last Post: deanhystad
  Am I wrong? Or is the question setup wrong? musicjoeyoung 3 3,463 May-18-2020, 03:38 PM
Last Post: musicjoeyoung
  What is wrong with my code? Than999 1 3,126 Nov-10-2019, 08:59 PM
Last Post: ichabod801
  Wrong output on my code. JTNA 2 9,199 Apr-04-2019, 01:55 PM
Last Post: JTNA
  Why is this code wrong? Lemmy 4 6,220 Apr-05-2018, 03:46 PM
Last Post: Lemmy
  What's wrong with my code and visuals for python? beginnercoder04 2 3,540 Mar-17-2018, 01:06 AM
Last Post: beginnercoder04

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020