Home > OS >  my dob code wont recognize days above 12 without triggering my input error text
my dob code wont recognize days above 12 without triggering my input error text

Time:04-12

my first code i have written whilst learning, and have become stuck on one issue.

    NAME=str(input("Enter your name: "))
    print ("hello",NAME)

    from datetime import *


    today = date.today()
    print("Today: "    today.strftime('%A %d, %b %Y'))
    good_value = False
    value = ""

    while good_value == False:
        value = input("Insert Date in format dd/mm/yyyy: ")

        try:
            datetime.strptime(value, '%m/%d/%Y')
            good_value = True

        except ValueError:
            print("Error: Date format invalid.")


    thisYear = today.year
    dob_data = value.split("/")
    dobDay = int(dob_data[0])
    dobMonth = int(dob_data[1])
    ÁdobYear = int(dob_data[2])
    dob = date(thisYear,dobMonth,dobDay)

    if today == date(thisYear,dobMonth,dobDay):
            print ("happy bithday", NAME)
    else:
            print ("its not your birthday, sucks to be you")

when i run the code it will work perfectly unless i type the date being over the 12th, so not breaking the error loop and obviously limiting the dates that can be put into the finished product.

CodePudding user response:

Here

value = input("Insert Date in format dd/mm/yyyy: ")

you are informing user that format should be DD/MM/YYYY, but here

datetime.strptime(value, '%m/%d/%Y')

you are expecting MM/DD/YYYY. In order to repair this replace former using

value = input("Insert Date in format mm/dd/yyyy: ")

and

dobDay = int(dob_data[0])
dobMonth = int(dob_data[1])

using

dobDay = int(dob_data[1])
dobMonth = int(dob_data[0])

Observe that this did worked for values equal or less than 12 as there are 12 months

  • Related