isinstance() does not produce the expected result after a data entry

Asked

Viewed 113 times

2

I made the following example of factorial using function isinstance().

  def factory(n):
       if not isinstance(n,int) or n<0:
            print('NAO E NUMERO INTEIRO VALIDO:\n')
            return None
       if n==0:
            return 1
       else:
            return n*factory(n-1)

Now when I try to make simple codes to understand how this function works I’m not getting a result, if I type integer number it doesn’t even enter the if. I would like to better understand the isinstance()

  x=input()

  if isinstance(x,int):
       print("ok!")
  • 1

    input returna str, vc have to convert x = int(input())

  • of course! , our stupid question. Thank you very much !!!!!

2 answers

3


That’s right, if you look at documentation of input() will see that it results in a string, then it wouldn’t make any sense to go into if.

You’ll probably want to turn this into a whole with the function int(), which will ensure that it is a given of the type int and the if does not make sense. Unfortunately the python culture is to let an exception explode if you cannot do the conversion, then the correct way to deal with it is to have a try-except in the code to check whether or not it worked (example), and maybe that’s what you really want.

Certainly isinstance() is not for what you want, it makes more sense when you have hierarchy of types in what you want to verify, or at least when you have real reason not to know what the exact type of data is , if you know what type it is its use makes no sense.

I have seen a lot of people confuse die type with its content. People often want a string that has only numeric digits is a number, and if you do not have a point it must be an integer. This is a serious error. In fact all input and output data with integer with the user is done by texts, which happen to be numerical digits, but are not numbers. Numbers exist by themselves and are used for calculations. The input and output is just a textual representation of the number that humans understand best, but it’s different than what the computer can handle. It’s just a fluke that this text has only numerical digits. When entering data through the console you need to convert to number to use as number, and it may be that this operation is not successful if the text is not well formed. And the print() also converts the number to a text, even if you do not notice, but it is a text printed there and not the number, which would be incomprehensible to a human.

1

Victor, as you can see on isinstance documentation(), it takes two parameters in its parentheses: the first is the variable or data you want to check and the second the type you want to make sure it is. In the case of isinstance(x,int), it will check if x is of type int; if yes, it will return TRUE and enter if.

Your question should be: I type an integer number in input() and the isinstance() returns false? Yes and the justification is that the input always returns a string. If you type an int number and want to use it in isinstance() what you should do is force it to be a number using int().

You can try the following:

x = int(input())

As you are studying, take a test:

x = input()
print(type(x))

will return 'str'

y = int(input())
print(type(y))

return 'int'

I hope I’ve helped!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.