0

Как вывести функцию is_number, которая будет принимать строку в качестве параметра и возвращать True, если эта строка является допустимой нотацией действительного числа, или же, в противном случае false. Выглядеть должно так:

def is_number(string: str) -> bool:

>>> is_number('3')

True

>>> is_number('3.3')

True

>>> is_number('6e-34')

True

>>> is_number('0.0.0')

False

>>> is_number('five')

False

raise NotImplementedError()

1 ответ 1

3

Как вариант:

def is_number(value: str) -> bool:
    try:
        float(value)
        return True
    except ValueError:
        return False

print(is_number('3'))  # True
print(is_number('3.3'))  # True
print(is_number('6e-34'))  # True
print(is_number('0.0.0'))  # False
print(is_number('five'))  # False

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.