All Python errors
ValueError: invalid literal for int() with base 10
This message means int() received text that does not look like a whole number; that text is shown at the end. To fix it, use float() for numbers with a decimal point, clean the text, or check it before converting.
Code with the error
weight = "12.5"
print(int(weight) * 2)
Error message (Python 3.12, last line)
Corrected code
weight = "12.5"
print(float(weight) * 2)
Output
25.0
Common causes
- The text has a decimal point: "12.5".
- The text is empty.
- There are letters, units or a comma: "5 kg", "1,000".
- A word is converted instead of digits: "ten".
Practise in the course
Lesson: String MethodsFrequently asked questions
How do I check that text is a whole number?
text.isdigit() is True for a non-empty string of digits. It is False for a minus sign.
Do spaces around the number matter?
No. int(" 7 ") works; spaces or letters inside the number do not.