Review: Built-In Functions Problems


#1
def distance_from_zero(a):
      if isinstance(a, float) or isinstance(a, int):
         return abs(a)
  else:
        return "Nope"

This fails with this: “Your function seems to fail on input True when it returned ‘1’ instead of ‘Nope’” I get that this is because bool is a subclass of int but how else would I solve this problem?

The provided solution (when I click “Get code”) does not work either, it has the same error.


#2

Hi @rxcs,

The problem is that True, which is of type bool is also an instance of int because the type bool is derived from the int type. Therefore, this is a better test for the type of a in this exercise …

  if type(a) == int or type(a) == float:

#3

Hi, as I said in my original post this solution does not work.


#4

What solution did it give you when you clicked “Get code”?


#5

It gave me something like this:

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

Which, as I said in the main post, fails with “Your function seems to fail on input True when it returned ‘1’ instead of ‘Nope’”

All you have done is repeat stuff I have already said in my main post–unhelpful!


#6

The code in your most recent post should produce correct results. If you received a message to the contrary after submitting that exact code, it likely indicates a bug in Codecademy’s submission correctness test (SCT) for this exercise.


#7

When I copy and paste it into Atom it works fine, but not in CodeCademy.


#8

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