#A function to input a date: John A. Oakey 2017 from datetime import date #set variables for max and min year month and day minyr, maxyr, minday, maxday, minmo, maxmo = 1900,2050,1,31,1,12 # this function will handle errors expected def dateerror(inpart, datepart, thisdatein): if datepart == "tryerror": print(thisdatein + " is not a valid date. Please try again.") return() if datepart == 'xxxx': print (inpart + ' must be integers in this form: 00/00/0000. Please try again.') return() print(str(inpart) +" is not recognized as a "+ datepart + " that I can use. Please try again." ) return() #this function gets and vets the date - must be in the form mm/dd/yyyy def getdate(): #set up our global variables global yr global month global day #set up and implement while loop FetchAGoodDate = True while FetchAGoodDate: print("Please input a valid date between " + str(minyr) + " and "+ str(maxyr)) date1=input("Please enter your date in the form mm/dd/yyyy: ") # now look for problems with the input, check for minimum possible length if len(date1)<8: dateerror(date1,'input','date') continue #...and tease out the values for month, day and year yr=date1[-4:] boundry=date1[-5] boundry2loc=date1.find(boundry,3,len(date1)) month=date1[0:date1.find(boundry)] day=date1[date1.find(boundry)+1:boundry2loc] #continue checking validity of all date components if (not yr.isdigit()) or (not month.isdigit()) or (not day.isdigit()): dateerror(date1,"xxxx",'date1') continue elif ((int(month)<minmo) or (int(month) > maxmo)): dateerror(month,"month","code2") continue elif((int(day) < minday) or (int(day) > maxday)): dateerror(day,"day","code3") continue try: testdate = date(int(yr), int(month), int(day)) except ValueError: dateerror(yr, "tryerror", date1) continue #all error possibilities tested, so accept our date data and end loop FetchAGoodDate = False return(date1) #proof that everything works datein = getdate() print("----------------------------------") print("the date in: ", str(datein)) print("month", str(month)) print('day ',day) print ("year input is: " +yr) dateobj = date(int(yr),int(month),int(day)) print ("date object: ",dateobj) print("date object for month: ",dateobj.month)