Search code examples
pythonstringint

Python --> TypeError: unsupported operand type(s) for -: 'int' and 'str'


HELP, my program is to say what year you are born and then tell you how old you are but I get:

TypeError: unsupported operand type (s) for -: 'int' and 'str'

If someone tells me why or me send the code well done I would appreciate it very much, thanks.

import  datetime
year = int(input("in what year were you born? ---> "))
yearac = datetime.date.today().strftime("%Y")
print(year)
print(yearac)
difere = year - yearac
print("You have",(difere),"years")

Solution

  • It is because yearac is a string. Try int(datetime.date.today().strftime("%Y"))