4

I need to write a function called day_of_the_year that takes a month and day as input and returns the associated day of the year. Let the month by a number from 1 (representing January) to 12 (representing December). For example:

day_of_the_year(1, 1) = 1
day_of_the_year(2, 1) = 32
day_of_the_year(3, 1) = 60

Use a loop to add up the full number of days for all months before the one you're interested in, then add in the remaining days. For example, to find the day of the year for March 5, add up the first two entries in days_per_month to get the total number of days in January and February, then add in 5 more days.

So far, I have made a list:

days_per_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

This is my code so far:

def day_of_the_year(month,day):
    days_per_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    for number in day_of_the_year:
        total = days_per_month[0] + days_per_month[1] + 5
        return total

Is there something that I am missing?

Michael M.
  • 10,486
  • 9
  • 18
  • 34
roTenshi2
  • 57
  • 6

3 Answers3

1

Yes, you are exiting your loop on the first pass every time.

What you should do is define the total variable outside of the for loop and then increment it on each iteration.

You also only need to iterate to the month that is specified so use the range function to loop. And since day_of_the_year is the name of the function, it will cause an error if you try to put it in a for loop.

Then once the loop has finished you can add the days to total and return it.

def day_of_the_year(month,day):
    days_per_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    total = 0
    for number in range(month - 1): 
        total += days_per_month[number]
    return total + day

print(day_of_the_year(1,1))
print(day_of_the_year(12,25))

output:

1
359

Michael's solution is the better solution to getting the answer, I just want to help you understand what you were missing and how to make it work.

Alexander
  • 16,091
  • 5
  • 13
  • 29
  • I appreciate the explanation. Could you explain why you entered month - 1? And why you are adding the days_per_month ? Also, this doesn't seem to work for the input (1,5)? – roTenshi2 Oct 26 '22 at 01:57
  • @roTenshi2 When I put `1,5` it outputs `5`. and january fifth is the fifth day of the year i used the `month -1` because arrays are zero indexed so leaving it as just month would count one too many. And I am adding the number in `days_per_month` that is indexed at `number` . – Alexander Oct 26 '22 at 02:00
  • `days_per_month[0] == 31` and `days_per_month[1] == 28` – Alexander Oct 26 '22 at 02:04
1

The easiest way to solve this problem is to use sum() to find the number of days in the months that have already passed, then add the extra days. Like this:

def day_of_the_year(month, day):
    days_per_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    return sum(days_per_month[:month-1]) + day


print(day_of_the_year(1, 1))  # => 1
print(day_of_the_year(2, 1))  # => 32
print(day_of_the_year(3, 1))  # => 60

It is more readable and fairly understandable. There's no need to worry about loops (which can be confusing for beginners).

Michael M.
  • 10,486
  • 9
  • 18
  • 34
  • What exactly does the colon before month mean? I am a beginner and havent learned that yet. – roTenshi2 Oct 26 '22 at 02:04
  • It's called [slicing](https://duckduckgo.com/?q=python+list+slicing) – Ignatius Reilly Oct 26 '22 at 02:05
  • That is how you indicate that you want to slice the list from the begining to whatever comes after the colon – Alexander Oct 26 '22 at 02:05
  • It gets a slice of the list. `days_per_month[:month-1]` means "make a new list with all the elements of `days_per_month` up to the `month-1`nth element". – Michael M. Oct 26 '22 at 02:06
  • I see now, thank you for the insight! – roTenshi2 Oct 26 '22 at 02:14
  • 1
    This is a better way, but it seems like it's a contrived assignment focused on loops. If it weren't for the assignment I'd suggest using datetime and bypassing the need to use a hardcoded list of month days and less worry about leap year accuracy. Then assuming we're working in the current year, we just need to take the delta between the last day of the previous year and the specified date. [Like this](https://onecompiler.com/python/3ym2p36kw). – shawn caza Oct 26 '22 at 02:15
0

Would it be acceptable if your loop was inside a list comprehension statement?

If we assume we are working in the current year than we can use datetime to get the current year. Rather than hardcoding days for each month we could use the calendar library. 'calendar.monthrange(year, month)' returns a tuple with the element at index 1 representing the number of days in the specified month. With those pieces it's fairly simple to make a list with the day numbers of previous months and sum them up.

from datetime import datetime
import calendar


def day_of_the_year(month, day):
    year = datetime.now().year
    days = sum([calendar.monthrange(year, m)[1] for m in range(1, month)]) + day
      
    return days
    
print(day_of_the_year(1, 1))  # => 1
print(day_of_the_year(2, 1))  # => 32
print(day_of_the_year(3, 1))  # => 60
shawn caza
  • 342
  • 2
  • 13