Python programming- Program should accept time and date strings like mentioned b
ID: 3753377 • Letter: P
Question
Python programming-
Program should accept time and date strings like mentioned below from the user in the console and then return appropriate output. Please provide properly formatted code. Thanks
1b) is leapday: This function should accept time-and-date strings formatted like"May 8, 2009 1:09:59am" and return None if the date+time is invalid (that date and time did not exist or is not written in the proper format), False-as in this example-if the time does not fall on a leap day, or True if it does fall on a leap day-as in "Feb 29, 2000 12:00:00pm. The month codes are Jan, Feb, Mar, Apr, May, Jun, JulAug, Sep, Oct, Nov, Dec.Explanation / Answer
This is the complete working code:
def isInt(s):
try:
int(s)
return True
except ValueError:
return False
def isProper(time):
s = time.split(':')
b = False
if (len(s)==3):
if (isInt(s[0]) and isInt(s[1]) and isInt(s[2][:-2])):
if ((int(s[0])<=12) and (int(s[0])>=0) and (int(s[1])<60) and (int(s[1])>=0) and (int(s[2][:-2])<60) and (int(s[2][:-2])<=0) and (s[2][-2:].lower() == 'am' or s[2][-2:].lower() == 'pm')) :
b = True
return b
def is_leapday(inp):
# May 8, 2009 1:09:59am
s = inp.split()
if (len(s)!=4):
print('invalid input')
return
month = s[0].lower()
day = s[1][:-1]
year = s[2]
time = s[3]
list_months = ['jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec']
if (month not in list_months) or (not isInt(year)) or (not isInt(day)) or not isProper(time):
print('invalid input')
return
if (int(year)%4==0 and int(year)%100!=0 or int(year)%400==0) and (month=='feb') and (int(day)==29):
return True
else:
return False
s = input("Please enter the day: ")
print(is_leapday(s))
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.