Wats wrong?


#1


def distance_from_zero(n):
if type(n)== int or float:
return "abs"
else:
return "Nope"


Replace this line with your code.


#2

That is what is wrong. Your function is returning a string when a number is expected. abs is a built-in function, abs(), that takes a number, in your case, n.

return abs(n)

would be the implementation.


#3

def distance_from_zero(n):
    if type(n)== int or float:
        return abs(n)
    else:
        return "Nope"

#4

still showing error help?


#5

What is the error message?


#6

Oops, try again. Your function seems to fail on input True when it returned '1' instead of 'Nope'


#7

Ah yes, we've seen this before. It looks like the test for True is included in the SCT. Python treats a boolean as an int in the type function, so returns 1.

Since your are using type(), we can just add on another operand:

    if type(n) == int or float and not bool:

>>> distance_from_zero(True)
'Nope'
>>>

The following is an isinstance() implementation, but I would use the above since it doesn't introduce code you haven't studied yet.

    if type(n) == int or float and not isinstance(n, bool):

#8

def distance_from_zero(n):
if type(n)== int or float and not bool:
return abs(n)

distance_from_zero(True)
'Nope'


#9

still not moving ahead???z?


#10

Can you post a link to this exercise, please? Thanks.


#11

https://www.codecademy.com/courses/python-beginner-c7VZg/4/3?curriculum_id=4f89dab3d788890003000096


#12

The older Python of the learning environment must be rejecting the logic. I couldn't get it to work so reverted to the more explicit form:

    if type(n) == int or type(n) == float and not type(n) == bool:

print distance_from_zero(-9.5)    # 9.5
print distance_from_zero(True)    # Nope

#13

did not get the print format?????


#14

You should get this in the console:

9.5
Nope
None

#15

https://www.codecademy.com/courses/python-beginner-c7VZg/4/3

still showing error


#16

Let's get one last look at your code. While you're at it, and after you copy and save it, refresh the window and submit again.


#17

Oops, try again. It looks like you have an error in your code. Check the error message for more info! - maximum recursion depth exceeded while calling a Python object


#18


#19

Your original code was not that far off, but I must have mislead you into thinking I was posting solution code, when I was actually only posting line code for you to insert or substitute into it. Go back to what you had at the beginning and make the necessary adjustments. You still need the code construct.


#20

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.