19. Review: Built-In Functions


#1

Hey,

Here is my code plus the error message I'm getting:

What could I be doing wrong? :confused:


#2

abs() is a function that returns the absolute value of whatever is placed inside the parentheses. In this case we want the absolute value of m, so we would write return abs(m)


#3

That solved it! Thanks! :relieved:


#4

Hello , Can anyone help me with this. Why am I getting a "none" here ??


#5

Your function is printing "nope" when it should be returning "nope".


#6

Even when I return "nope" why is it printing "none" in the console ??


#7

Have you tried putting nope in quotes i.e. "Nope" ?


#8

The extra None that is printed in the console is a result of how Codecademy processes your code. If you were to run the same code in another interpreter you would not see that None. If you would like to test this you can use the labs repl.it


#9

Exactly ! That's what my question is.. I don't get it in other
interpreters. How am I supposed to complete this exercise now? And
continue with the course. Please give some suggestion.


#10

The problem is with your if statement. Currently you have:

if type(d) == int or float:

And I understand why it would be tempting to write it that way, but with programming we need to be very specific so it actually needs to be written as:

if type(d) == int or type(d) == float

Also, as @muzakir said, make sure you have:

else:
    return 'nope'

#11

Thank you so much mkordik. It's solved but I still don't understand why it didn't throw a syntax error or something


#12

The syntax wasn't wrong in terms of Python being able to process it. It was wrong because it wasn't checking what you really wanted to check.

Here is a good post on why it did what it did:
https://www.codecademy.com/en/forum_questions/5187c9af569b6ae7ab004fae


#13

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

And Voila!


#14

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