-1

I want to check my Datatype in python using if else

Output I want : If user Enter float Datatype so it print It is float or if user enter another Datatype so it print it is not float

Code:

c=(input("Enter the value\n"))
if type (c) == float:
    print('it is float')
else:
   print("it is not float")

Output I want:

Enter the value
12.1
it is float

Output I'm getting:
If I enter float datatype it is still printing it is else

Enter the value
12.1
it is else
  • 1
    I think you misunderstand. The `input()` function *always* returns an object of type `str`. You can convert it to other types yourself if you want to. – quamrana Aug 14 '21 at 15:32
  • 1
    `c` is always `str` – balderman Aug 14 '21 at 15:32
  • Does this answer your question? [How to check if input is float or int?](https://stackoverflow.com/questions/59807810/how-to-check-if-input-is-float-or-int) – quamrana Aug 14 '21 at 15:34
  • so what can I do? if I use ```c=float(input("Enter a number")``` so it always show float. Any solution? –  Aug 14 '21 at 15:34

3 Answers3

1

You should have something like this

c = (input("Enter the value\n"))
try:
    f = float(c)
    is_float = True
except ValueError:
    is_float = False
balderman
  • 22,927
  • 7
  • 34
  • 52
1
c=input("Enter the value\n")
try:
    float(c)
    print('it is float')
except ValueError:
   print("it is not float")

your problem is that input always give you str value so by trying to convert it to float you know if its a float or not

yotam rec
  • 587
  • 4
  • 15
0

Check if object is int or float: isinstance()

The type of an object can be obtained with the built-in function type().

i = 100
f = 1.23

print(type(i))
print(type(f))
# <class 'int'>
# <class 'float'>

The built-in function isinstance(object, type) can be used to determine whether an object is of a particular type.

Get / determine the type of an object in Python: type(), isinstance()

print(isinstance(i, int))
# True

print(isinstance(i, float))
# False

print(isinstance(f, int))
# False

print(isinstance(f, float))
# True

In this case, since only the type is checked, it cannot be determined whether the value of float is an integer (the fractional part is 0).

f_i = 100.0

print(type(f_i))
# <class 'float'>

print(isinstance(f_i, int))
# False

print(isinstance(f_i, float))
# True
source: check_int_float.py
Check if float is integer: is_integer()
float has is_integer() method that returns True if the value is an integer, and False otherwise.

f = 1.23

print(f.is_integer())
# False

f_i = 100.0

print(f_i.is_integer())
# True

For example, a function that returns True for an integer number (int or integer float) can be defined as follows. This function returns False for str.

def is_integer_num(n):
    if isinstance(n, int):
        return True
    if isinstance(n, float):
        return n.is_integer()
    return False

print(is_integer_num(100))
# True

print(is_integer_num(1.23))
# False

print(is_integer_num(100.0))
# True

print(is_integer_num('100'))
# False

Credits: https://note.nkmk.me/en/python-check-int-float/

  • It is absolutely the case that one should use `isinstance()` rather than checking the return of `type()`. Thanks for bringing that up. However, in this case `input()` always returns a string so we can't really use `isinstance()` in the way we hope and a cast to float can throw an error so we really need an answer built around `try:` – JonSG Aug 14 '21 at 16:01