Fou89
Fou89

Reputation: 21

Python datetime and finding birthyear

I am trying to make small program where I can find the birth year, given the age,

import datetime
x=raw_input("your age is?")
y=datetime.datetime.now().strftime("%Y")
print y-x

However, this is not working because of the format of the output.

Upvotes: 0

Views: 473

Answers (3)

Garrett Hyde
Garrett Hyde

Reputation: 5597

Your input and current year need to be integers.

from datetime import datetime

x = int(raw_input("What is your age? "))  # Get user's age
y = datetime.now().year                   # Get current year

print("Your birth year: {}".format(y-x))  # Print difference

Upvotes: 0

Tim Pierce
Tim Pierce

Reputation: 5664

The most straightforward way to get this should be

y = datetime.datetime.now().year

Upvotes: 2

UltraInstinct
UltraInstinct

Reputation: 44444

strftime(..) returns a string. You need to convert into an int.

y = int(datetime.datetime.now().strftime("%Y"))

Upvotes: 0

Related Questions