# Program to calculate number of days in a year

Page 1 of 1

## 3 Replies - 11225 Views - Last Post: 17 March 2010 - 05:51 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=161433&amp;s=ce26a88936fd60b3d0f401d3b3d28cac&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 femilangy

Reputation: 0
• Posts: 2
• Joined: 10-March 10

# Program to calculate number of days in a year

Posted 11 March 2010 - 03:43 PM

Hello all,
I am trying to write a python program to calculate the number of days in a year,by reading through a text file and telling if the date is valid(and if valid,list the number of days in the year),or invalid.I have come up with the folowing code but i am kinda stuck from here on how to calculate the number of days.i will appreciate if someone can look through my codes and help modify or add where necessary.thanks

```def isleapYear(year):

if year % 400 == 0:
return True
elif year % 100 == 0:
return False
elif year % 4 == 0:
return True
else:
return False

def validate(m,d,y):

if m == 4:
d == 30
return True
elif m == 6:
d == 30
return True
elif m == 9:
d == 30
return True
elif m == 11:
d == 30
return True
elif m == 2:
if isleapYear(year):
d == 29
return True
else:
d == 28
return True
else:
d == 31
return True

def dayNum(m,d,y):
dayNum = 31(m - 1) + d
if m == 2:
dayNum == (4m + 23)/10
return dayNum

```

i am stuck on how to go about invoking the last two functions in def dayNum(m,d,y),and calculating the number of days.
Thanks peeps

ESPECIALLY since Python is whitespace sensitive, code tags are very important.

This post has been edited by Dogstopper: 12 March 2010 - 04:32 PM

Is This A Good Question/Topic? 0

## Replies To: Program to calculate number of days in a year

### #2 Steven Smith

Reputation: 6
• Posts: 45
• Joined: 17-March 08

## Re: Program to calculate number of days in a year

Posted 13 March 2010 - 01:28 PM

Good start! I see it is your first post as well. Welcome, and please browse around and consider joining the community after your question is answsered.

def isleapYear(year):
- No Issues Seen

def validate(m,d,y):
- Your d == (##) statements generate a T/F that is not returned or assigned anywhere.
- You probably meant if (m == 4) & (d <= 30): return True ... etc.
- When you get to February, you can just add an additional if statement where you check d <= 28/29
- As an added note, I would open this function by type checking for integers (IE. if type(d) == 'int': continue, else return false), as well as checking for months outside the range of 1-12 and days outside the range of 1-31.

Number of days in a year. To strictly answer your question, you would just check if it is a leap year or not. If it is numDays = 366, if not... 365.
However, based on your code attempt, I think you are trying to calculate the DAY of the year given a date, IE Feb 2nd would be the 33rd day.

def dayNum(m,d,y):
- First, remember that multiplication in Python (and most langugages) require a multiplication operator, or * in this case. So 4*m not 4m.
- The code quality and logic degraded so fully in this section that I wouldn't feel right giving you code... but I will give you an algorithm.

First, if allowed, you can use the existing time and date libraries to avoid recreating the wheel.

If not, you can use an interative approach. If this is month 8, I need to find the number of days in months 1-7 and add the current day number. Use a list/array to hold the values for all 12 months assuming no leap year. If it is month 8, use a loop to check the number of days in that month and add it to the total. IE... x += monthDays[x]. Afterward, if dayNum > dayNum of Feb 29th and isLeapYear == True... add one day.

Try this and post again if you still need help.

This post has been edited by Steven Smith: 13 March 2010 - 01:30 PM

### #3 femilangy

Reputation: 0
• Posts: 2
• Joined: 10-March 10

## Re: Program to calculate number of days in a year

Posted 14 March 2010 - 01:43 PM

Thanks steve,i have been working on editting the codes and i came up with this fo the first two functions,i am still working on the number of days in a year.you can go through and see if i need some more adjustments.
thanks again.
```import string
def isleapYear(year):

if year % 400 == 0:
return True
elif year % 100 == 0:
return False
elif year % 4 == 0:
return True
else:
return False

def validate(date):
moNum = [0,31,28,31,30,31,30,31,31,30,31,30,31]
date = "m/d/y"
mdy= string.split(date,"/")

if len(mdy) != 3 : return False
try:
month = int(mdy[0])
day = int(mdy[1])
year = int(mdy[2])
except: # integer conversions failed.
return False
if year < 0 : return False
if month < 1: return False
if day < 1 :  return False
if month >12: return False
if month == 2 and isLeapYear(year) and day <=29: return (month,day,year)
elif day <= moNum[month]:
return (month,day,year)
else:
return False

```

### #4 Steven Smith

Reputation: 6
• Posts: 45
• Joined: 17-March 08

## Re: Program to calculate number of days in a year

Posted 17 March 2010 - 05:51 PM

Making progress...

```
moNum = [0,31,28,31,30,31,30,31,31,30,31,30,31]
date = "m/d/y"
mdy= string.split(date,"/")

```

moNum... Pretty sure you added the zero at the beginning to just "make it work" and avoid the confusion of the first month being [0], etc. Just making sure you understand why this occurs.
date = ... By doing this, you basically overwrite the variable the function received. Take this line out.
mdy = ... string.split(date, "/") doesn't compile for me. I used str.split(date, "/") Might be a version thing though... Nice usage of the function though.

The rest of the code looks pretty good at a glance. Take a stab at the next part and I'd be happy to help with that as well.