0

OK, so I have to build a program which works out if someone is older than 18, and thus if they can vote.

However, I'm struggling with how to get the program to calculate whether the date of birth entered is more than 18 years ago. I've tried various things, including taking away their date of birth from today's date.

I think it would be easier being able to change the difference between their date of birth date which is currently in datetime.timedelta form, to an integer which can then be compared to 18 in an if statement.

I don't want anything too complicated please.

Here's my current code:

from datetime import datetime
while True:
    inp = input("Enter date in format yyyy/mm/dd")
    try:
        then = datetime.strptime(inp, "%Y/%m/%d")
        break
    except ValueError:
        print("Invalid input")
diff = then - datetime.now()
diff = -diff
years = diff/365.25
years = int(years)
if years >= 18:
    print("You can vote")
else:
    print("You can't vote")

The current problem is years = int(years) which was a lame way of trying to change it.

How can I fix this?

martineau
  • 119,623
  • 25
  • 170
  • 301
Beastly Gerbil
  • 225
  • 5
  • 18
  • Take a look at the [`dateutil`](http://labix.org/python-dateutil#head-ba5ffd4df8111d1b83fc194b97ebecf837add454) library, `relativedelta(datetime.now(), then).years` should give you what you're after. – asongtoruin Dec 13 '16 at 14:29
  • @asongtoruin but I want the user to input their date, not it already entered – Beastly Gerbil Dec 13 '16 at 14:29
  • You have read the input to a variable which is formatted as a `datetime`- from that point onwards, the questions are functionally identical – asongtoruin Dec 13 '16 at 14:36
  • What's lame is trying to determine someone's current age by dividing by 365.25 (and then using that to determine if they can vote in the next election—when they'll be a different age). – martineau Dec 13 '16 at 15:30

3 Answers3

1

relativedelta from the dateutils library will do exactly what you're after - by specifying .years we output just the number of years between two dates.

from datetime import datetime
from dateutil.relativedelta import relativedelta


while True:
    inp = input("Enter date in format yyyy/mm/dd: ")
    try:
        then = datetime.strptime(inp, "%Y/%m/%d")
        break
    except ValueError:
        print("Invalid input")

difference_in_years = relativedelta(datetime.now(), then).years

if difference_in_years >= 18:
    print("You can vote")
else:
    print("You can't vote")
asongtoruin
  • 9,794
  • 3
  • 36
  • 47
0
# Python 2.7.10 
from datetime import datetime
while True:
    inp = raw_input("Enter date in format yyyy/mm/dd") # 1987/03/29
    try:
        then = datetime.strptime(inp, "%Y/%m/%d")
        break
    except ValueError as e:
        print e
        print("Invalid input")
diff = then - datetime.now()
diff = -diff

print dir(diff)
print diff.days

years = float(diff.days)/365.25 
if int(years) >= 18:
    print("You can vote")
else:
    print("You can't vote")

# 29.711156742 
>>> You can vote

# Python 3.5.2 
from datetime import datetime
while True:
    inp = input("Enter date in format yyyy/mm/dd")
    try:
        then = datetime.strptime(inp, "%Y/%m/%d")
        break
    except ValueError as e:
        print (e)
        print("Invalid input")
diff = then - datetime.now()
diff = -diff
years = float(diff.days)/365.25 
years = int(years)
if years >= 18:
    print("You can vote")
else:
    print("You can't vote")

>>> You can vote
Ari Gold
  • 1,528
  • 11
  • 18
0

I you see years = int(years) as problem, skip it. That won't do any harm.

then = datetime.strptime(inp, "%Y/%m/%d")
diff = datetime.now() - then
if diff.years/365.25 >= 18:
    print('You can vote.')
else:
    print('You can\'t vote')