0

I'm trying to make a function "is_float" which can test if a value is a float or an iterrable which contains only floating point number.

for just one value, I did this (it works fine to me):

def is_float(value: Any) -> bool:
    try:
        float(value)
        return True
    except ValueError:
        print(f'Cannot convert "{value}" to a floating point number.')
        return False

But I also tried this with an itterable :

def is_float(value: Iterable[Any]) -> bool:
    try:
        for item in value:
            float(item)
        return True
    except ValueError:
        print(f'Cannot convert "{item}" to a floating point number.')
        return False

And of course... it is not working if value is not an "iterable" but just an int or a string for exemple.

Is there a simple way to do that otherwise than adding an if statement to check if the iterable contain only one value ?

Thanks !

Riddeen
  • 9
  • 3

0 Answers0