1

I'm trying to make an age calculator for python and I have a problem with subtracting the user's input date of birth and today's date. I have tried float but it doesn't work. I tried subtracting the variables itself but that doesn't work, either.

age_str = input ("Enter your birthday on dd-mm-yy Format:")```

age = datetime.datetime.strptime(age_str, '%d-%m-%Y')```

today_str = datetime.date.today()```

today = datetime.datetime.strptime(today_str, '%d-%m-%Y')```

total = age - today```
General Grievance
  • 4,555
  • 31
  • 31
  • 45
  • If you report on code that fails it is always a good idea to post the error message. – Christian K. Mar 12 '20 at 12:45
  • I think your age variable should actually be the birthday variable because you are determining the age by subtracting `age = today - birthday` or of course use absolute value of the subtraction. Perhaps this post could be helpful. https://stackoverflow.com/questions/8419564/difference-between-two-dates-in-python – Daniel Gale Mar 12 '20 at 13:13

1 Answers1

-1
from datetime import date

def calculate_age(born):
    today = date.today()
    return today.year - born.year - ((today.month, today.day) < (born.month, born.day))
aamirmalik124
  • 125
  • 15
  • While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – Igor F. Mar 13 '20 at 07:21