TypeError for non-integer input (#9250)

* type error check

* remove str input

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Md Mahiuddin 2023-10-10 10:19:40 +06:00 committed by GitHub
parent 7b996e2c22
commit 4f8fa3c44a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -16,7 +16,15 @@ def num_digits(n: int) -> int:
1 1
>>> num_digits(-123456) >>> num_digits(-123456)
6 6
>>> num_digits('123') # Raises a TypeError for non-integer input
Traceback (most recent call last):
...
TypeError: Input must be an integer
""" """
if not isinstance(n, int):
raise TypeError("Input must be an integer")
digits = 0 digits = 0
n = abs(n) n = abs(n)
while True: while True:
@ -42,7 +50,15 @@ def num_digits_fast(n: int) -> int:
1 1
>>> num_digits_fast(-123456) >>> num_digits_fast(-123456)
6 6
>>> num_digits('123') # Raises a TypeError for non-integer input
Traceback (most recent call last):
...
TypeError: Input must be an integer
""" """
if not isinstance(n, int):
raise TypeError("Input must be an integer")
return 1 if n == 0 else math.floor(math.log(abs(n), 10) + 1) return 1 if n == 0 else math.floor(math.log(abs(n), 10) + 1)
@ -61,7 +77,15 @@ def num_digits_faster(n: int) -> int:
1 1
>>> num_digits_faster(-123456) >>> num_digits_faster(-123456)
6 6
>>> num_digits('123') # Raises a TypeError for non-integer input
Traceback (most recent call last):
...
TypeError: Input must be an integer
""" """
if not isinstance(n, int):
raise TypeError("Input must be an integer")
return len(str(abs(n))) return len(str(abs(n)))