def distance_from_zero(n):

if type(n)== int or float:

return "abs"

else:

return "Nope"

`Replace this line with your code.`

def distance_from_zero(n):

if type(n)== int or float:

return "abs"

else:

return "Nope"

`Replace this line with your code.`

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.

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

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

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):`

def distance_from_zero(n):

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

return abs(n)

distance_from_zero(True)

'Nope'

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
```

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.

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

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.

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