-1

I am trying to test a simple function to get a specific day of the week given a date, but every time I input the arguments the return value keeps coming out of none. As shown below, the code is not complete, but I am using a very specific date (Jan. 3, 1900) to get a desired output: Wednesday; this is in fact the weekday of that specific date.

 def isYearLeap(year):
    return year % 4 == 0 and (year % 400 == 0 or year % 100 != 0)

    def dayOfYear(year, month, day):
        century = int(str(year)[:2])
        if century in [15, 19, 23]:
            century_code = 3
        elif century in [16, 20, 24]:
            century_code = 2
        elif century in [17, 21, 25]:
            century_code = 0
        elif century in [18, 22, 26]:
            century_code = 5
        yr = year % 100
        b = yr // 12
        c = yr % 12
        d = c // 4
        sum = century_code + yr + b + c + d
    
        if sum >= 7:
            while sum >= 7:
                sum -= 7
    
        days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
        if sum in [0, 1, 2, 3, 4, 5, 6, 7]:
            for day in range(0, 8):
                if sum == day:
                    **year_dday = days[day]**
    
        if month == 1 and isYearLeap(year) == False:
            if day in [3, 10, 17, 24, 31]:
                **return year_dday**
    
    print(dayOfYear(1900, 1, 3))

Visualization of the code and "Return value: None"

2 Answers2

0
   def isYearLeap(year):
    return year % 4 == 0 and (year % 400 == 0 or year % 100 != 0)
    def dayOfYear(year, month, day):
        century = int(str(year)[:2])
        if century in [15, 19, 23]:
            century_code = 3
        elif century in [16, 20, 24]:
            century_code = 2
        elif century in [17, 21, 25]:
            century_code = 0
        elif century in [18, 22, 26]:
            century_code = 5
        yr = year % 100
        b = yr // 12
        c = yr % 12
        d = c // 4
        sum = century_code + yr + b + c + d
        if sum >= 7:
            while sum >= 7:
                sum -= 7
        days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
        if sum in [0, 1, 2, 3, 4, 5, 6, 7]:
            for day in range(0, 8):
                if sum == day:
                     year_dday = days[day]
                if month == 1 and isYearLeap(year) == False:
                    if day in [3, 10, 17, 24, 31]:
                        return year_dday
    print(dayOfYear(1900, 1, 3))
0

In your example day=7 so the code never runs the return statement. You should decide what to return in this case and implement it at the end of your function.

Note that for this task the datetime module will probably do a better job. See here: How do I get the day of week given a date?

Flostian
  • 93
  • 1
  • 7
  • Thanks, I didn't realize that the "day" parameter in my "for statement" changed the original input parameter. Also, I would have used a module, but I'm doing a course and need to do it without using one. – Chandler Juego Aug 28 '20 at 13:36