TypeError: can only concatenate str (not "int") to str
You used + between a string and a number. Python will not guess whether you meant text or arithmetic.
What it means
+ means concatenation for strings and addition for numbers, and Python refuses to mix them. The message tells you the left side is a str and the right side is an int. The reverse order, 5 + "5", gives "unsupported operand type(s) for +: 'int' and 'str'" — same bug, other way round.
Common causes
1. Building a message with a number in it
The most common case — gluing a count or score into a sentence.
Breaks
print("You scored " + score + " points")Works
print(f"You scored {score} points")2. input() always returns a string
Even when the user types 5, input() gives you "5". Adding to it is concatenation, not arithmetic.
Breaks
age = input("Age: ")
print(age + 1)Works
age = int(input("Age: "))
print(age + 1)3. Numbers read from a file or CSV
Everything read from text is text until you convert it.
Breaks
total = ""
for line in f:
total = total + line.strip()Works
total = 0
for line in f:
total += int(line.strip())How to find it in your own code
Decide what you want: text or a number. For text, use an f-string — it converts automatically and reads better than str() everywhere. For arithmetic, convert the string with int() or float() as early as possible, right where it enters the program.
Try it: fix this bug
easyCalling format_price(5) crashes with: TypeError: can only concatenate str (not "int") to str.
def format_price(amount):
return "$" + amountStill not sure why yours breaks?
Paste it into the visualizer and watch it run line by line, with every variable at every step. Free, and it runs in your browser.