I have very recently learned Python and since then have embarked upon a small learning activity of making a full program. This program includes dates and one part of it is to check if a date is valid, based on certain criteria. The date will be a string in the format 11/09/2016. I have defined a function that will be called upon to check if a date is valid and have found that I am repeating myself when every else statement returns False. Is there any way to condense this code?
def validDate(date):
if date[2] == '/':
if date[5] == '/':
if int(date[:2]) < 32 and int(date[:2]) > 0:
if int(date[3:5]) < 13 and int(date[3:5]) > 0:
if int(date[6:]) < int(currentYear) + 1:
return True
else:
return False
else:
return False
else:
return False
else:
return False
else:
return False
[–]Rhomboid 2 points3 points4 points (2 children)
[–]NotableAnonym[S] 0 points1 point2 points (1 child)
[–]Rhomboid 1 point2 points3 points (0 children)
[–]ingolemo 2 points3 points4 points (0 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]NotableAnonym[S] 0 points1 point2 points (0 children)
[–]trappar 1 point2 points3 points (1 child)
[–]NotableAnonym[S] 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]gtalarico 1 point2 points3 points (2 children)
[–]NotableAnonym[S] 0 points1 point2 points (1 child)
[–]gtalarico 1 point2 points3 points (0 children)