Oct-22-2021, 09:45 PM
You can shorten it up a bit more. There's no need for the "present" variable of the final "else". It is also a good idea to provide guidance on what is the proper date format.
from datetime import datetime def enter_date(prompt, valid): '''Enter date string. Return reformatted datestring and flag indicating if date is in the past''' while True: date = input('Provide date (m/d/yyyy): ') try: date = datetime.strptime(date, '%m/%d/%Y') except ValueError: print("Incorrect date format, please enter a proper date.") else: if date.date() >= datetime.now().date(): return date.strftime('%m/%d/%Y') print("Date must not be in the past.") print(enter_date())