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?