19 - Review:built - in functions


#1

I'm stuck as to why it won't work. Someone please help! :cry:

def distance_from_zero(elephant)
if type(elephant) == int:
return abs(elephant)

elif type(elephant) == float:
    return abs(elephant)

else:
    return ("Nope")

#3

@mightybraveknight

I suggest you properly codify your code first, by highlighting it all and hitting the </> key above the post area or hitting ctrl+alt+c while it's highlighted or using markdown.

Example:

```python

# CODE HERE

``` # END

Doing this will ensure that we as a community can read your code as white-space indentations is 100% important in python.

Next you should also post the ROI of your task so that we can help you, it's been a while so I don't remember what the task is and the same is most likely true for anyone who can give you useful help.

Finally, from what I can see you are trying to tell the distance from zero for any number?

Just create a simple function to do so,

def distance_from_zero(number):
    try:
        return abs(number)
    except TypeError:
        return "Failed on input!"

If that is not what you are looking for please ask again, with the appropriate information so that we can help you.


#4

Thank you for those tips, here is the code again:

def distance_from_zero(elephant):
    if type(elephant) == int:
        return abs(elephant)

    elif type(elephant) == float;
        return abs(elephant)

    else:
        return ("Nope")

#5

Done it now; thank you to @zeziba and @amanuel2 for helping me out.

Here is a cookie, make sure to share it.

:cookie:


#6

I'm not sure if this is the only problem, but after there is a ";" at the end of the elif statement.

edit: so I just tried it out, if you change the semi-colon to a colon you should be set.


#7

Here is how I coded.
def distance_from_zero(hello):
if type(hello) == int or type(hello) == float:
return abs(hello)
else:
return "nope"
It worked.


#8

EL problema puede ser las " ", aveces no te deja pasar si no usas las ' '.
Otra cosa, que escribas correctamente la palabra, None está con la N en mayúscula, prueba escribiendo 'none' en minúscula. A mí me funcionó este código:

def distancia_desde_cero(i):
    if type(i)== int or type(i)== float:
        #print abs(i)
        return abs(i)
    else:
        return 'none'
distancia_desde_cero("Oso")