19. Review: Built-In Functions. Should it return "None" in the console?


#1


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

CodeAcademy says my code is fine actually, but shouldn't my code return either the absolute value of the argument, or the word "Nope"?


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


#2

The reason None or undefined is seen on the console can be found here (credits to haxor), this might be for JS, but is the reason for python.

The thing with the return is that it returns something when the function is called:

distance_from_zero(3)

now, there are two things you can do, use a print statement so that whatever is returned is printed:

print distance_from_zero(3)

or you can store the result (which is returned) into a variable:

store = distance_from_zero(3)

then print it:

print store

the second method is useful when you want to do something with the data returned from the function, not needed in this case, but i would like to show it either way


#3

I'm sorry, but I am still unclear as to why it doesn't return Nope or the absolute value? As I understand it, it's something to do with the fact that CodeAcademy uses a console?


#4

There are two things: None being printed by the console on codecademy. Which i linked to another topic which explains this in detail. One more thing: None and "nope" are NOT the same thing

Then, i tried to explain that return returns something when the function is called. You need to use print on the function call:

print distance_from_zero(3)

in other to print something to the console window