Module 2 · Interaction with the user · Lesson 4 of 6
Type casting
The cure for that TypeError is type casting: deliberately converting a value from one type to another. To turn a string of digits into a real integer, pass it through int().
int() turns text into a number
"5"→int( )→5
int("5") + 2=7— it works now
There are three you'll use constantly: int() makes a whole number, float() a decimal, and str() turns a number back into text. So the standard recipe for reading a number is to wrap the input: int(input(...)).
Run it yourselfruns in your browser · Python
main.py
One caution: the text has to actually look like the target type. int("5") is fine, but int("hello") — or int("5.5") — raises a ValueError, because there's no whole number in there to find.