1

Error

Traceback (most recent call last):
  File "C:\Users\Alex\Python\Lesson01.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")
  • 1
    Does this answer your question? [How can I read inputs as numbers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-numbers) – VirtualScooter Feb 19 '21 at 22:38

2 Answers2

1

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

Henrik
  • 828
  • 8
  • 34
1

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.

PlatPlayZ
  • 19
  • 6