python - Code for checking leap year and counting no of days in a month -


this code:

def cal(month):     if month in ['january', 'march','may','july','august','oct','dec']:        print ("this month has 31 days")     elif month in ['april','june','sept','nov']:        print ("this month has 30 days")     elif month == 'feb' , leap(year)== true:        print ("this month has 29 days")     elif month == 'feb' , leap(year) == false:         print ("this month has 28 days")     else:         print ("invalid input")      def leap(year):     if (year % 4== 0) or (year % 400 == 0):        print ("its leap year!")     else:         print ("is not leap year")  year = int(input('type year: ')) print ('you typed in :' , year) month = str(input('type month: ')) print ('you typed in :', month)    cal(month) leap(year)  

the output i'm receiving:

type year: 2013 typed in : 2013 type month: feb typed in : feb not leap year not leap year invalid input not leap year 

why not getting output count of days in feb if 28 day month or 29?

why getting invalid input part though else?

you shouldn't try use own function replace basics one. python has module manage dates. should use it.

https://docs.python.org/2/library/calendar.html#calendar.monthrange

>>> import calendar >>> calendar.monthrange(2002,1) (1, 31) 

and leap year, still in doc :

https://docs.python.org/2/library/calendar.html#calendar.isleap 

regarding code, function leap should return boolean since use in condition statement, otherwise leap(whatever) == true return false.


Comments