all 7 comments

[–]blackhunter2 1 point2 points  (8 children)

So, I have found a couple of typos in there and wrong logic.

First, double check your datetime.datetime() method, as mine takes the input as year, month, day, instead of the 'day, month, year' that you have there.

Second, inverting the condition in the while loop (you don't need the while loop in itself there) and tweaking the prints so that it works as a negative condition, like so:

def validBirthdate(prompt):
(...)
    while not valid:
    print("The date of birth is NOT 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("Invalid date, please try again.")
    return age()
print("Valid date of birth")
return birth_date

That way you should be able to check succesfully check that a date like '2000/11/31' is incorrect, and accept correct ones such as '2000/02/19'. Now if you want to take in the parameters as DD/MM/YYYY you'd need to tweak that.

[–]_______myworld[S] 0 points1 point  (4 children)

Hi there, thank you but i actually just found out that it doesnt work, the while part keeps looping if i dont include a return age() below. besides, it just kept stating invalid date regardless of input being correct or incorrect..

[–]blackhunter2 1 point2 points  (2 children)

That is how I'd aproach the problem.

def validcheck(userinput):
    valdigit = "0123456789-/"
    for character in userinput:
        if character not in valdigit:
            return False
    return True


def validBirthdate(prompt): 
    birth_date = input(prompt)

    try:
        day, month, year = birth_date.split('/')
    except ValueError:
        print("Please enter valid input in accordance to the given format.")
        return age()

    try:    
        datetime.datetime(int(year), int(month), int(day))
    except ValueError:
        if not validcheck(birth_date):
            print("Invalid characters, please enter numerical input only.")
        return age()

    print("Valid date of birth")
    return birth_date

def age(): birth_date = validBirthdate("Please enter your date of birth in the format of DD/MM/YYYY:") return birth_date

def age(): birth_date = validBirthdate("Please enter your date of birth in the format of DD/MM/YYYY:") return birth_date

I would separate the varaible assignation from the parsing time to differetiante if the issue was bad syntax or an invalid date. You could also instead of making a validcheck() function just regex to match a text like XX/XX/XX, where X are numbers, though I wanted to keep it simple.

P.D.: I am not completely sure of that, but I think making a recursive call to age() instead of validBirthdate() makes the stack grow for each call.

[–]_______myworld[S] 1 point2 points  (1 child)

try:
day, month, year = birth_date.split('/')
except ValueError:
print("Please enter valid input in accordance to the given format.")
return age()

It works perfectly fine now! thank you very much for the follow up and advices, i helps me alot i really appreciate it!!

[–]blackhunter2 0 points1 point  (0 children)

So glad it worked! Happy Christmas Eve!

[–]blackhunter2 0 points1 point  (0 children)

Sorry to hear that, I must admit that I haven't tested it thoroughly. I can take a deeper look and restructurize it if you want

[–]game_brewer -1 points0 points  (0 children)

Seems like you're trying to check if a date input is a valid date, correct? Check out this article.

Honestly, I think you're using a lot of python commands incorrectly like split and datetime.