I am getting an operand error in pycharm, and I don’t understand why because in pycharm instant feedback sense, the code is correct.
What is your name?
How old are you?
Traceback (most recent call last):
File "/Users/tiacobb/PycharmProjects/CobbMod2/NameAge.py", line 5, in <module>
year = date.today().year - age
~~~~~~~~~~~~~~~~~~^~~~~
TypeError: unsupported operand type(s) for -: 'int' and 'str'
Please help. Thank you.
Tried:
from datetime import date
name = input("What is your name?")
age = input("How old are you?")
year = date.today().year - age
print("Hello" + name + "!" + "You were born in" + str(year) + ".")
user25121123 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
input()
function returns a string, while date.year
is an integer. You should convert your input to integer to make proper calculation, like this:
age = int(input("How old are you?"))
That should fix your error. You also might want to add some error handling in case your input will not be convertible to integer.
BTW error you encountered is not a PyCharm error, it’s just pure Python error. I encourage you to read documentation about topics related to your question: input function and TypeError.