Let's look at your code:
int(result)
All that will do is raise an exception if result
cannot be converted to an int
. It does not change result
. Why not? Because in python string (and int) objects cannot be changed, they are immutable. So:
if isinstance(result,str):
print('finished')
this test is pointless, because result
will always be a str
because you have not changed it - that's the type returned by input()
.
The way to deal with error messages is to fix or handle them. There are two general approaches, "look before you leap" and "exception handling". In "look before you leap" you would check to see if result
can be turned into an int
by using a string test like str.isdigit()
. In python the usual way is to use exception handling, for example:
result = input('Type in your number,type y when finished.\n')
try:
# convert result to an int - not sure if this is what you want
result = int(result)
except ValueError:
print("result is not an int")
if isinstance(result, int):
print("result is an int")
You can see I specifically tested for ValueError
. If you don't have this and just have except
then it would trap any error, which could mask other issues.