Traceback (most recent call last) Python Error

Question:

Error

Traceback (most recent call last):
  File "C:UsersAlexPythonLesson01.py", line 5, in <module>
    print("In 10 years you will be ", a+b, "years old")
TypeError: can only concatenate str (not "int") to str

At the moment I am Learning Python 3 and got this error. What does it mean? I also tried + instead of the comma this is also not working :/

Here is the code:

user_input = input("How old are you?n-> ")

a = user_input
b = 10
print("In 10 years you will be ", a+b, "years old")
Asked By: user15244524

||

Answers:

You have to print this:

user_input = int(input("How old are you?n-> "))

a = user_input
b = 10
print("In 10 years you will be " + str(a+b) + " years old")

this should work fine

Answered By: Henrik

The other answer by u/Henrik is correct but I would argue some better naming schemes would improve the readability and usefulness of the code…

age = int(input("How old are you?n-> "))

numYears = 10

print("In " + str(numYears) + " years you will be " + str(age+numYears) + " years old")

The point of this way is to make the print statement more versatile, also avoid having duplicate variables where it isn’t necessary, and to have better variable names.

Answered By: PlatPlayZ
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.