I am trying to take date input from users in dd/mm/yyyy format and check if user has entered the date correctly and then print it. If user does not enters the date correctly, program gave error message and ask again till date is entered correctly.
In the following code, if user input the date correctly in first try the value from function dateInput() gets passed on to variable 'd' correctly. However, the problem is that if date is entered wrongly in first try then even if date is entered correctly than dateInput() function is not returning the date to the variable 'd'.
from datetime import datetime
def dateValidation(date_text):
try:
datetime.strptime(date_text, "%d/%m/%Y")
return True
except ValueError:
return False
def dateInput():
input_date = input("Enter a date in dd/mm/yyyy format:")
if dateValidation(input_date) == True:
return input_date
else:
print("Incorrect date, Please try again")
dateInput()
d = dateInput()
print(d)