Review : Built-in Functions / distance_from_zero


#1

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.

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

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)
    else:
        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)
exit()


#2

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


#3

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
far.

Thanks for your help, it is much appreciated !
Marc

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:
arg1=int(arg1)
return abs(arg1)

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

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

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


#4

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.


#5

Ok Thanks again !

Have a great evening.

Marc


#6

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