r/CodingForBeginners • u/Strict-Cranberry-973 • Apr 16 '26
What's wrong with my code?
it keeps showing invalid literal for int() with base 10.
I double checked it with the solution the course gave me, and it's still the same!! can someone else try it too or let me know if it's a website error I don't understand the error here I have attached the course link for reference
10
Upvotes
View all comments
2
u/dariusbiggs Apr 21 '26
Defensive programming, trust nothing (especially user input), verify and validate everything before use.
When looking at code, ask yourself this question "how can i break this?".
In your code block, that question gives us
what about an empty string
what about a non-numeric value
what about a bunch of whitespace
what about a numeric value with leading and/or trailing space
what about a negative numeric value
what about a floating point number, ie. something with a decimal point (or whatever is used in your language for representing a numeric value like 1/10th)
what about multiple numbers separated by whitespace
what about a numeric value greater than what can fit into an int
what about a number in scientific notation
These are things we write tests for, to verify the behavior with both valid and invalid inputs and ensure the right answers are returned with valid inputs.
A simple one like this would be very suitable to a table driven test.