'i need help adding the leap year functionality into my python program [closed]

import math    
repeat = True    
date = raw_input('Enter a date in the format MM/DD/YYYY') #Prompts user to input    

while repeat:
   date_month = date[0:2] #Takes the input's month and keeps its value for the varible date_month    
   date_day = date[3:5] #Takes the input's day and keeps its value for the varible date_day    
   date_year = date[6:10] #Takes the input's year and keeps its value for the varible date_year       
   if 00 < int(date_month) < 13:    
      if 00 < int(date_day) < 32:    
         if 0000 < int(date_year) < 2017:    
               date = raw_input('The date you entered is valid, enter another date in the format MM/DD/YYYY')               
   else:    
      date = raw_input('invalid date found! Please enter another date in the format MM/DD/YYYY')


Solution 1:[1]

Roll-your-own parser code is silly. Python has batteries included for this:

import datetime

repeat = True    
datestr = raw_input('Enter a date in the format MM/DD/YYYY')

while repeat:
    try:
        # Parse to datetime, then convert to date since that's all you use
        date = datetime.datetime.strptime(datestr, '%m/%d/%Y').date()
    except ValueError:
        pass  # Handle bad dates in common code
    else:
        if 0 < date.year < 2017:
            datestr = raw_input('The date you entered is valid, enter another date in the format MM/DD/YYYY')
            continue  # Bypass invalid date input common code

    # Both exception and invalid years will fall through to this common code
    datestr = raw_input('invalid date found! Please enter another date in the format MM/DD/YYYY')

Obviously, as written, this doesn't actually terminate the loop under any conditions, but neither does your original code. The advantage here is that strptime does the heavy lifting; it validates many more things that your original code, through a single try/except, and handles tricky stuff like days of the month by month without special checks. You can access the year, month and day attributes of the date object it parses as before, as Python native ints, no individual int conversions as you go.

Note that if you wanted to use locale appropriate date representations, the format you chose happens to exactly match for en_US locale, and you could make it more portable to non-U.S. users by just using datetime.datetime.strptime(datestr, '%x') (though you'd have to make the raw_input prompt dynamic to match); that works the same as '%m/%d/%Y' for en_US, but switches ordering and separators for, say, a German locale, which would make it equivalent to '%d.%m.%Y'.

Solution 2:[2]

It is as simple as this:

>>> import calendar
>>> print(calendar.isleap(1999))
False
>>> print(calendar.isleap(2004))
True

Solution 3:[3]

There a more problems with the question than just if the year is leap or not. @Dex'ter solution will indeed tell you if year is leap or not, but thing is you can't really ask for that in the end. So your first question should be is the user input date a leap year:

 if calendar.isleap(date_year):

, followed by month for which your code is functional. And than you)ll have another problem in the day.

 if 00 < int(date_day) < 32: 

This will give you erroneous results no matter if the year is leap or not because there are plenty of months than don't have 31 days. For that I would advise:

 >>> from calendar import monthrange
 >>> monthrange(2011, 2)

EDIT: and by the way this last function supports leap year as well.

Solution 4:[4]

I rewrite your program.I wish it will help

import math
from datetime import datetime
repeat = True    
date = raw_input('Enter a date in the format MM/DD/YYYY :') 
while repeat:
    try:
        datetime.strptime(date, "%m/%d/%Y")
        date = raw_input('The date you entered is valid, enter another date in the format MM/DD/YYYY :')
    except ValueError:
        date = raw_input('invalid date found! Please enter another date in the format MM/DD/YYYY :')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 armatita
Solution 4 Harun ERGUL