
While I was writing a tip calculator in Python, you can check my GitHub for the full code, I realized that even though I used an if condition, errors were still happening.
The reason is that the if condition runs after the type conversion, but the error happens during the conversion itself.
The key point is that float(value) is a risky operation. If the conversion fails, Python throws an error before the if condition is even checked.
Using value.isdigit() may look safe, but it fails for valid inputs like 12.5, -3, or even 10 with spaces. This is why try/except exists.
Always use if to validate rules, and try/except to protect your program from crashing.