Review : Built-in Functions / distance_from_zero


Whatever version of code, I've been through several iterations : it always shores-up with this last "error"
-> Oops, try again. Your function seems to fail on input {1: 2} when it returned 'None' instead of 'Nope'
From what I can see, it is trying to send 2 variables - or something completely out of line with the actual exercise.


This is my code and tests that I ran independently to ensure it trapped all the normal possibilities
def distance_from_zero(n):
    if type(n) == int or type(n) == float:
        return abs(n)
        return 'Nope'

print distance_from_zero(2)
print distance_from_zero(-2)
print distance_from_zero(0)

print distance_from_zero(2.1)
print distance_from_zero(-2.1)

print distance_from_zero("banana")
print distance_from_zero(False)
#print distance_from_zero(2:1)


Remove that line. Your code does return 'Nope' when a dict ({1: 2} ) is passed in.


Hi Roy,

Is it normal that we need to filter out dictionary-type of data ?
Doesn't this go above and beyond what we have learned up until now ?

Also, since I ran it - with the lines included - I thought I was all set.
What else would be needed and that was seen/included in the lessons thus

Thanks for your help, it is much appreciated !

PS : since at first I couldn't figure it out, I also tried a ton of
variants, like the more cumbersome :

def distance_from_zero(arg1):

if type(arg1) is int:
return abs(arg1)

if type(arg1) is float:
return abs(arg1)

if type(arg1) is bool:
return "Nope"

if type(arg1) is str:
return "Nope"


We didn't have to filter out the dict. It returns 'Nope' since a dict is not an int or a float. From what I can tell, it was the exit() function that returned None.


Ok Thanks again !

Have a great evening.



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