+ 1
how to check specific type of variable
i've tried : x = 3 if x == int: print("it is interger") else: print('it is not interger") it give me an error. is there any method to do this ?
8 Respostas
+ 6
x = 5
if type(x) == int:
print("integer")
else:
print("not an integer")
you missed the type() function
0
to check the type you can use isinstance function. your x == int can be replaced with
isinstance(x,int)
in the similar way you can check other types (float, str or classes)
0
@francisco you got the point its not the problem
0
@sundar thanks a lot man !
0
just type in type <>
- 1
The second print() begins with a single quote and ends a double quotes. There is your problem. 😋
- 1
@Francisco Gomez. you found syntax error,
I found semantic error :-)
even he change it to double quotes he need to check the type by comparing so. the condition always be else block, even it's an integer.
- 1
@RedAnt. Thanks man I didn't know it.