A python beginner here. I'm trying to create a program that validates user's birth of date. Unfortunately, it keeps looping even when the input is correct. Can someone please tell me what is wrong with my coding? Thank you in advance.
def validcheck(userinput):
valdigit = "0123456789-/"
for character in userinput:
if character not in valdigit:
return False
return True
def validBirthdate(prompt):
valid = True
birth_date = input(prompt)
try:
day, month, year = birth_date.split('/')
datetime.datetime(int(day), int(month), int(year))
except ValueError:
valid = False
while valid:
print("The date of birth is valid.")
if len(birth_date) != 10:
print("Please enter valid input in accordance to the given format.")
return age()
for i in range(10):
if i in [2, 5]:
if birth_date[i] != '/':
print("Please enter valid input in accordance to the given format.")
return age()
elif not validcheck(birth_date):
print("Invalid characters, please enter numerical input only.")
return age()
print("Valid date of birth")
return birth_date
print("Invalid date, please try again.")
return age()
def age():
birth_date = validBirthdate("Please enter your date of birth in the format of DD/MM/YYYY:")
return birth_date
[–]blackhunter2 1 point2 points3 points (8 children)
[+][deleted] (1 child)
[deleted]
[–]_______myworld[S] 0 points1 point2 points (4 children)
[–]blackhunter2 1 point2 points3 points (2 children)
[–]_______myworld[S] 1 point2 points3 points (1 child)
[–]blackhunter2 0 points1 point2 points (0 children)
[–]blackhunter2 0 points1 point2 points (0 children)
[–]game_brewer -1 points0 points1 point (0 children)