Ex7-12 (P.231)

Write a function to test whether a given date is valid. For example 5/24/1962 is valid, but 9/31/2000 is not. (September has only 30 days.)

Note: If you want to verify the input numbers are integers instead of floating point numbers, you may use the built-in function isinstance().

You may test your function with the following main program.


print("This program will verify whether a date is valid.")
s = input("Input a date (month/day/year) -- ")
monthStr, dayStr, yearStr  = s.split("/")
month = eval(monthStr)
day = eval(dayStr)
year = eval(yearStr)
if ValidDate(month, day, year):
    print(s, "is a valid date.")
else:
    print(s, "is an invalid date.")