Interaction with the user

Module 2 · Interaction with the user · Lesson 3 of 6

Prohibited operations

Now the trap springs. Because input() always gives you a string, trying to do maths with it goes wrong. Python flatly refuses to mix text and numbers — and says so, loudly.

Text + number doesn't add up
"5" (str) + 2 (int)
TypeError: can only concatenate
str (not "int") to str

Adding the string "5" to the integer 2 doesn't make 7. To Python, + means “add” for numbers but “join” for text — and it can't do either across the two types. The result is a TypeError.

Run it yourselfruns in your browser · Python
main.py

This is the classic beginner bug: read a number with input(), try to add to it, and get a TypeError you didn't expect. The fix is to convert the text into a real number first — which is exactly what the next lesson is for.