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.
input returna str, vc have to convert x = int(input())
– Elton Nunes
of course! , our stupid question. Thank you very much !!!!!
– Vitor Gonçalves