int() converts a string to an integer only if the string is exactly an integer: digits, an optional sign, nothing else. "3.5", "1,000", "" and "abc" all fail with this message — and the message quotes the offending string, which is the first thing to look at.
int(float("3.5")) gives 3. If you actually need the fraction, keep it a float and don’t convert at all.text = raw.strip() removes stray spaces and newlines; then wrap the conversion in try/except ValueError and ask again.int("1,000".replace(",", "")). Data from spreadsheets and web pages is full of these.If the string looks like a clean number and still fails, suspect invisible characters — a non-breaking space or a BOM copied from a web page. Print `repr(text)` to see the actual characters; repr never lies.
Why does int("3.0") fail? It’s basically 3. Because int() parses integer syntax only, and "3.0" isn’t it. int(float("3.0")) is the standard route.
What’s the safest pattern for converting user input? try/except around the conversion: handle the failure where it happens, with a message the user can act on. Checks like isdigit() miss negative numbers and decimals, so try/except is the honest tool.