Jump to content

Can anyone tell me what is wrong with this python code?

pipnina

I have been using CodeCademy to learn Python for a while now. But this one task seems to be impossible! I feel like I've tried everything.

 

Can anyone help me?

 

 

def distance_from_zero(Z):

    E = "Nope"
    if type(Z) == int or type(Z) == float:
        return abs(Z)
    elif type(Z) == str:
        return E
 
distance_from_zero(Z = raw_input())

 

post-60781-0-10045000-1416319873_thumb.p

Link to comment
Share on other sites

Link to post
Share on other sites

I have been using CodeCademy to learn Python for a while now. But this one task seems to be impossible! I feel like I've tried everything.

 

Can anyone help me?

 

True is a boolean value

 

instead of else if just use else ;)

Mini-Desktop: NCASE M1 Build Log
Mini-Server: M350 Build Log

Link to comment
Share on other sites

Link to post
Share on other sites

True is a boolean value

 

instead of else if just use else ;)

Unfortunately that gives me a syntax error.

FYI, this uses python 2 instead of 3.3, IDK why.

Link to comment
Share on other sites

Link to post
Share on other sites

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

or even just

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

Mini-Desktop: NCASE M1 Build Log
Mini-Server: M350 Build Log

Link to comment
Share on other sites

Link to post
Share on other sites

Unfortunately that gives me a syntax error.

FYI, this uses python 2 instead of 3.3, IDK why.

 

just use "else:" instead of "elif type(Z)==str"

elif type(Z)==str

looks if Z is a string

It might very well be another data type (like e.g. a boolean one). then  the function doesn't return anything! (which is very bad)

Mini-Desktop: NCASE M1 Build Log
Mini-Server: M350 Build Log

Link to comment
Share on other sites

Link to post
Share on other sites

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

or even just

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

 

 

Cheers!

 

 

I changed it to that and it works now!

 

I guess I'm going to need to be careful around this sort of picky thing in the future...

Link to comment
Share on other sites

Link to post
Share on other sites

Cheers!

 

 

I changed it to that and it works now!

 

I guess I'm going to need to be careful around this sort of picky thing in the future...

 

 

be sure to check my second answer out, os you understand why your original solution works in some cases, but not in all ;)

Mini-Desktop: NCASE M1 Build Log
Mini-Server: M350 Build Log

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×