Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

Greetings this is a piece of code that should make a function that profits the total estimation of the inputted number or float. Can't sort out what's going on with it, Look at the code and the error. Any assistance is valued! 

Look at the accompanying code for function:

import math

def distance_from_zero(num):

    type_entry = type(num)

    if type_entry == int:

        return math.abs(num)

    elif type_entry == float:

        return math.abs(num)

    else:

        return "Not an integer or float!"

Here is the place where I tried out the code by printing the outcome

print distance_from_zero(4)

Look at the error which occurs.

Traceback (most recent call last):

  File "python", line 12, in <module>

  File "python", line 5, in distance_from_zero

AttributeError: 'module' object has no attribute 'abs'

python 

1 Answer

0 votes
by (26.4k points)

Here, abs() is an implicit(built-in) function, so supplant all events of math.abs with abs

You ought to likewise utilize the isinstance() work for type checking as opposed to utilizing type() and looking at, for instance:

def distance_from_zero(num):

    if isinstance(num, (int, float)):

        return abs(num)

    else:

        return "Not an integer or float!"

Join the python online course fast, to learn python concepts in detail and get certified.

To know more about this you can have a look at the following video tutorial:-

Related questions

0 votes
1 answer
asked Sep 25, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Jan 14, 2021 in Python by ashely (50.2k points)
0 votes
4 answers
0 votes
1 answer
0 votes
1 answer

Browse Categories

...