Reputation: 13
Good afternoon all, I'm new to StackOverflow and Python in general. I'm taking a university course and I'm pretty stumped on pinning down an operand error I'm getting from a very simple calculate-and-return program:
currentYear = 2020
age = input("How old were you in 2020, in numbers? ")
birthYear = currentYear - age
print("Your birth year was: " + str(birthYear))
The error being returned is a TypeError: unsupported operand type(s) for -: 'int' and 'str'
I've tried removing the concatenation in the print, the casting to string, and I've tried putting the calculation of the variables "currentYear" and "age" as strings. I've also tried to force an integer input on the "age" variable by inserting the int(input()) format. It's a very basic issue that I'm overlooking, I'm sure, but I'm at my wits' end!
Upvotes: 1
Views: 41
Reputation: 2076
Welcome! Your issue here is that age
is a string, because the input
outputs whatever string the user gives. Fortunately you can explicitly make it into an integer, like so:
age = input("How old were you in 2020, in numbers? ")
birthYear = currentYear - int(age)
print("Your birth year was: " + str(birthYear))
Note: this completely ignores the case where your user makes a mistake and the value input is not a number. For example if they input "eight" or something. This is a whole different problem though!
Upvotes: 1