← Back to all posts
I am new to Python, will someone be able to figure out what this means

Traceback (most recent call last):
File "python", line 4, in <module>
TypeError: can only concatenate str (not "int") to str

this error showed up when i wrote:
NUMBER = int(input())
a = (NUMBER - 1)
b = (NUMBER + 1)
print ("The next number for the number" + (NUMBER) + "is" + (b))
print ("The previous number for the number" + (NUMBER) + "is" + (a))

Answered by RyanRana (189) [earned 5 cycles]
RyanRana (189)

What mwilki said was partially correct. For every integer item in the print stantment, you need to turn it into a string in order to get it in the same format as the rest of the strings. This is very easy to do.

print ("The next number for the number" + str(NUMBER) + "is" + str(b))
print ("The previous number for the number" + str(NUMBER) + "is" + str(a))

mwilki did this only with NUMBER, not with a & b. these two variables are based on the NUMBER variable, so they are integers to.

What lesson to take away from this: Whenever dealing with different types of data in python you always want to convert it to one. In this case, we converted NUMBER, a, and b to strings to print it out. But anyway just copy the code above into yours.

Please subscribe, python lessons beginner, intermediate, and advanced are included + projects!

Upvote this post if it helped!

HarryPall (1)

it did not work, still has same error

mwilki7 (993)

change

``````print ("The next number for the number" + (NUMBER) + "is" + (b))
print ("The previous number for the number" + (NUMBER) + "is" + (a))``````

to

``````print ("The next number for the number" + str(NUMBER) + "is" + (b))
print ("The previous number for the number" + str(NUMBER) + "is" + (a))``````