Python –> TypeError: unsupported operand type(s) for -: 'int' and 'str'
Question:
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")
Answers:
strftime
(as the name suggests) returns a str
ing, and even though you can cast it to an integer using int
like you’re doing with the input, you should use the year
attribute instead to get the year as an integer:
yearac = datetime.date.today().year
use yearac = datetime.date.today().year
It is because yearac is a string. Try int(datetime.date.today().strftime(“%Y”))
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")
strftime
(as the name suggests) returns a str
ing, and even though you can cast it to an integer using int
like you’re doing with the input, you should use the year
attribute instead to get the year as an integer:
yearac = datetime.date.today().year
use yearac = datetime.date.today().year
It is because yearac is a string. Try int(datetime.date.today().strftime(“%Y”))