Review: Built-In Functions


#1

I'm receiving here the error message:

Your function seems to fail on input True when it returned 'None' instead of 'Nope

My code looks like this:

> def distance_from_zero(x):
>     if type(x) == int or type(x) == float:
>         return abs(x)
>     else:
>         "Nope"
>         print distance_from_zero(25)

#2

I know that the line:

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

is problematic, since it evaluates up until "or." But the assignment seems to require that syntax.


#3

Hi @wmatthewrice ,

Because the code that you posted is not formatted, it is difficult for other users to read and debug it. After code has been pasted into the editing window for posting, you can format it by selecting it, and then by clicking the </> button above the editing area. Alternatively, you can place three backquotes on the line before the code and three backquotes on the line after the code. This will enable us to see important details, such as the indentation and underscores. If you use the backquotes, your code will be color-coded, making it especially easy to read.

This does not return the value, "Nope" ...

    else:
       "Nope"

Change the second line in the above to ...

        return "Nope"

#4

Perfect. Thanks for formatting information -- will use going forward -- and the syntax tip.