-2
def date(date):
    DD, MM, YYYY=date.split(' ')
    return datetime.date(int(YYYY),int(MM),int(DD))

while True:
    end=input('End Date (DD MM YYYY): ')
    end=date(end)
    if end[0:1].isdigit() and end[3:4].isdigit() and end[6:9].isdigit() and datetime.datetime.strptime(end, '%d/%m/%Y'):
           break
     else:
           print("Invalid")

Error: Traceback (most recent call last): File "C:\Users\NPStudent\Downloads\main.py", line 311, in if start[0:1].isdigit() and start[3:4].isdigit() and start[6:9].isdigit() and datetime.datetime.strptime(start, '%d/%m/%Y'): TypeError: 'datetime.date' object is not subscriptable

  • what error are you getting? – depperm Aug 04 '16 at 13:31
  • 1
    Possible duplicate of [How do I validate a date string format in python?](http://stackoverflow.com/questions/16870663/how-do-i-validate-a-date-string-format-in-python) – depperm Aug 04 '16 at 13:36
  • 1) Why are you trying to validate the return value of `date` *after* you call it, rather than validating it *inside* the function? 2) `end` is not a string anymore; you assigned a `datetime.date` object to the name. – chepner Aug 04 '16 at 13:43

1 Answers1

0

Not sure on the purpose of the date function, but one potential way of checking the input is:

import datetime

while True:
    end = input('End Date (DD MM YYYY): ')
    try:
        datetime.datetime.strptime(end,'%d %m %Y')
        print("Correct")
        #date function?
        break
    except ValueError:
        print("Invalid")
LukeBowl
  • 200
  • 2
  • 13