# validDate # # This function is given integer month day and year. It returns a string, # which is either the empty string or a message indicating that # either the month, day or year is out of range. def validDate(m,d,y): maxDay = [0,31,28,31,30,31,30,31,31,30,31,30,31] errorMsg = "" if y%400 == 0 or y%100>0 and y%4==0: # this is a leapyear if m == 2: maxDay[2] = 29 if m < 1 or m > 12: errorMsg = "Month out of range" else: upper = maxDay[m] if d<1 or d >upper: errorMsg = "Day out of range" else: if y < 1600 or y > 2200: errorMsg = "Year out of range" return errorMsg # testValidDate # # This is a test driver to allow repeated testing of the validDate # function. It allows the user to enter dates, and for each date # calls validDate. It reports the value validDate returned # before entering the next date. def testValidDate(): month = input("Enter a month(1-12), or 0 to quit: ") while month != 0: day = input("Enter the day: ") year =input("Enter the year: ") message = validDate(month,day,year) if message == "": print "Date is valid" else: print message month = input("Enter a month(1-12), or 0 to quit: ") testValidDate()