Python Check if Value is Float Continue if So

over 9 years

Did you write "if type(a) == int or float" and it's not working? See here

Many people have been writing the following code to check if the number entered is an integer or a float

                def distance_from_zero(a):     if type(a) == int or float:         ....              

But, if they enter something that isn't a number they get an error. The reason is that this if statement will always be True. This is because of a concept called TRUTHVALUES. When Python has an if statement it checks the TRUTHVALUES of the statements. Now almost all values in Python are True. The only values that are False are False, None, 0 or any equivalent number-type, and any empty container like [] () {} set() "" ''. Everything else is True.

Thus, when someone writes or float they are really writing or True. To get around this we would need to check each one individually like so -

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

There are other ways of doing this like if-in, but I'll leave you guys to figure that out.

Hope this helps! Cheers!

Answer 52485410abf82174930040e8

Sometimes the CodeAcademy Python Interpreter gets messed up. Actually not the interpreter because the following code works fine as far as the interpreter is concerned. It's the Code Academy AI that gets buggy and says nonsense like "Oops, try again! Your function seems to fail on input -10 when it returned 'None' instead of '10' "

That's when I try it on another interpreter… either on my local machine or on a server.

This code works fine despite what Code Academy says:

                def distance_from_zero(a):     if type(a) == int or type(a) == float:         print abs(a)     else:         print 'Not an integer or a floating point decimal!'  distance_from_zero(-5)              

And yes, substituting 'return' for 'print' works fine. So if the interpreter is happy, you should be too. Don't worry about the Code Academy buggy error messages.

points

about 9 years

Answer 542b926480ff3362dc000f20

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

This code works and upon clicking "Save and Submit" it will return as correct.

points

about 8 years

Answer 51fc10fd548c35cc1b000331

                def distance_from_zero(n): if type(n) == int:     return abs(n) elif type(n) == float:     return abs(n) else:     return "Not an integer or float!" distance_from_zero(10)              

This also works.

points

about 9 years

Answer 523aecd980ff33471300497d

it seems to be right:

                def distance_from_zero(s):     if type(s) == int or type(s) == float:         return abs(s)     else:         return 'Not an integer or float!' distance_from_zero(-10)              

points

about 9 years

Answer 53f92737548c35ddd800542b

So when does CodeAcademy plan on addressing this?

points

about 8 years

Answer 52f9e943282ae3a66a00154b

                def distance_from_zero(m):     if type(m) == int or type(m) == float:         return abs(m)     else:         return "Not an integer or float!"  print distance_from_zero(---3.34---------45)              

points

over 8 years

Answer 5340c710631fe90e610006cb

Seems like lots of confusion on this one, yet I found it to be one of the easiest in the whole lesson…

My code is below: (FYI: the "////" = spaces)

def distance from zero(t): ////if type(t) == int or type(t) == float: ////////return abs(t) ////else: ////////return "Nope"

Congratulations, you've finished this course! Hope this helps someone!

points

over 8 years

Answer 51939b44e541d5338e003a8f

if type(thing)==int or type(thing)==float: thing can be replaced by what parameter you are using.

points

over 9 years

Answer 51e052029c4e9d6261000059

But type() returns a string containing the text 'int' or 'string', not the type int or string. In the code below, I check the string against the type, and I am not checking if it is true or not. Can somebody please clarify why the following code does not work?

                                  def distance_from_zero(a):         if type(a) == 'int' or type(a) == 'float':             return abs(a)         else:             return "Not an integer or float!"  print distance_from_zero(-10)              

points

over 9 years

Answer 51ef6fa3282ae3b5be0021bd

I just wanted to say that Michael Rochlin's post helped so much, as did several other comments in this thread. I'm very new to programming and I can't imagine I would have ever figured out on my own why my code was working. Thanks!

points

Submitted by Troy

about 9 years

Answer 52331a25548c35b1b1003e33

Can someone please tell me why I am receiving an error message with the following code?

                def distance_from_zero(z):  if type(z) == int or type(z) == float:     return abs(z)  else:     return "Not an interger or float!"     distance_from_zero(1)              

points

about 9 years

Answer 52eaac57282ae3a931000411

Maybe I'm lazy, but why is there not a way to just put:

                type(a) == (int or float)              

or

                type(a) == [int or float]              

I suppose the if-in method is similar, but it just seems like one of those two would be more intuitive.

points

over 8 years

Answer 53571808548c352384000b82

I feel like I've tried every single solution posted but mine keeps returning "Oops, try again. Your function seems to fail on input True when it returned '1' instead of 'Nope'"-this is my code:

def distance_from_zero(x):

if type(x) == int or type(x) == float: return abs(x) else: return "Nope"

help :(

points

over 8 years

Answer 55108a889113cbb0a400089a

                def distance_from_zero(name): if type(name) == int or type(name) == float:     return abs(name) else:     return "Nope" **That is what I did it worked perfectly :)**              

points

over 7 years

Answer 555e10e2e39efeaa8c000503

If you still want to validate this exercise add at the beginning of your code :

                class str(object): def __add__(self, other):     return True              

points

over 7 years

Answer 5612714051b88718aa000227

points

about 7 years

Answer 51d65eb2631fe9608d014fb6

This code isn't working – please help!! I can't see what I am doing wrong :-/ ugh I feel like a total failure with these courses, I always have to come to the Q&A Because nothing work :(

                def distance_from_zero(x):         if type(x) == int or type(x) == float:             return abs(x)         else:             "Not an integer or float!"              

points

over 9 years

Answer 51f281ac631fe9a64c003341

`def distance_from_zero(thing): if type(thing) == (int or float): return abs(thing) else: return "Not an integer or float!"

distance_from_zero(9)`

Please ignore the format…can anyone tell me why isn't this code working? I remember another code where this works….thank you!

points

about 9 years

Answer 51f66421282ae3795e00d931

                (type(x) == int or type(x) == float)== True              

i know == True is unnecessary,but is it right by logical and syntax ?

points

about 9 years

Answer 51f84815548c353fec000171

When I enter a word as a parameter I get an error, but if I enter an integer, float, or string it passes.

Example: print distance_from_zero("hot") or distance_from_zero(67) it passes. Now try distance_from_zero(hot) and it gives an error.

Can anyone explain this to me? Thanks

points

about 9 years

Answer 52468cefabf821559a001470

def distance_from_zero(chisl): if (type(chisl) == float): return abs(chisl) elif (type(chisl) == int): return abs(chisl) else: return 'Not an integer or float!' print distance_from_zero(20)

points

about 9 years

Answer 5251e0b9548c35e114007b41

What's wrong with my code??? def distance_from_zero(a): if type(a) == int or type(a) == float: return abs(a) else: return 'Not an integer of a float!'

Thanks!!

points

about 9 years

Answer 52584361f10c602aab00002c

                def distance_from_zero(n): if n==type(int):     return abs(n) elif n==type(float)     return abs(n) else:     return "Not an interger or float!"              

help, why doesn't this work?

points

about 9 years

Answer 5274e5bb548c35770d002933

if I write a answer here,where will my answer appear?

points

almost 9 years

Answer 5278286d80ff336f260087b4

I tried:

                                  def distance_from_zero(d):         t = type(d)         if t == [int, float]:             a = abs(d)             return a         else:             return "Not an integer or float!"              

Any issue with negative numbers should have been taken cared of by abs(). However, even my code doesn't work.

points

almost 9 years

Answer 527d3ff7abf8219462000a2c

i dont know what im doing wrong….

def distance_from_zero(x): if type(x) !=str: if type(x) ==int: return abs(x) elif type(x) ==float: return abs(x) else: return "Not an integer or float!"

points

almost 9 years

Answer 529357bdf10c60760300147c

Just to clarify what Michael said, because I didn't get it before.

When you put if type(a) == int or float: python sees the "or" statement and compares [ type(a) == int ] with [ float ] so, that's why float is always True. Imagine the code like this:

if [type(a) == int] or [float]:

To clarify more, the statement: [if float] is always True because you don't compare it with something, float by it's own is float and logically just a True value by it's own.
Only if it was [If type(a) == float] could sometimes be False

points

almost 9 years

Answer 52a108218c1ccc233c00125f

This code worked! `def distance_from_zero(z):

                result=type_check(z) if result==1:     answer=abs(z)     return answer else:     return "Not an integer or float!"              

def type_check(chk): if type(chk)==int or type(chk)==float: return 1 else: return 0

distance_from_zero("True")`

points

almost 9 years

Answer 52a885a58c1cccb5a3001f94

a = raw_input("Enter anything you like: ") def distance_from_zero(a): if type(a) == int or type(a) == float: return abs(a) else: return 'Not an integer or float!'

distance_from_zero(a)

points

almost 9 years

Answer 52bdb14a9c4e9de4c3002b89

My code works:

import math def distance_from_zero(n): if type(n)==int or type(n)==float: return abs(n) else: return "Not an integer or float!"

distance_from_zero(-10)

points

almost 9 years

Answer 52bdb1a28c1ccc46620020ad

import math def distance_from_zero(n): if type(n)==int or type(n)==float: return abs(n) else: return "Not an integer or float!"

distance_from_zero(-10)

points

almost 9 years

Answer 52d463aa80ff33d06b009b17

YOU'RE A GENIUS! thank you! you explained this really well, i will remember why this is how it works from now on because of you!

points

almost 9 years

Answer 52fdcc318c1cccc8850012ad

This works fine, eventually. Though I do not want to input a string in ''. If there's anyone here who is able to give me a clue why I have to use '' when typing in strings, I'd be glad. However, if there's anyone giving me a clue on how to avoid having to use the '' and the raw_input() command, I'd be a happy bunny.

                n=input("Type something, nerd:") def distance_from_zero(n):     print type(n)     if type(n)==int or type(n)==float:         return abs(n)     else:         return "Not an integer or float!"  print distance_from_zero(n)              

points

over 8 years

Answer 530323c07c82cadb96000cc0

                def distance_from_zero(x): if isinstance(x, int) or isinstance(x, float):     return abs(x) else:     return "Not an integer or float!"              

This works IRL but in the editor it doesn't. It's a built in function that allows returns of correct true or false values based on the type validation.

edit: Python Docs on isinstance

points

over 8 years

Answer 53037f54282ae3ffa1003479

points

over 8 years

Answer 53050db29c4e9d4a3e0000be

I forgot my exclamation point in the message that is returned if its bad. Took me a while to realize!

points

over 8 years

Answer 531ac9479c4e9d4b88000cbd

This was the mistake that I made:

                                  if type(x) == int or float:              

But now I've got it to work. I changed it to:

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

The instruction above was helpful. Thanks.

points

over 8 years

Answer 535573a1631fe98ba8000fda

This works in Eclipse: """ distance from zero function """ def distanct_from_zero( arg ): if type(arg) == int or type(arg) == float: return abs( arg ) return "Nope"

But not in your interpreter. How do I get beyond this example??

points

over 8 years

Answer 5387bef49c4e9d62430014b1

You actually don't have to define the number for distance_from_zero(n) I don't know why everyone does this but its unnecessary as my code worked just fine…

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

points

over 8 years

Answer 53894d327c82cafe7800197a

After fooling with this for a very long time, it came to me that everything is some distance from everything else–even from zero.

points

over 8 years

Answer 5389b87e9c4e9d030d00230e

def distance_from_zero(s): if type(s) == int or type(s) == float: return abs(s) else: return 'Nope'

print distance_from_zero( raw_input( 'enter integer or float:'))

points

over 8 years

Answer 5390af8e9c4e9dd7e80000bc

Why won't this work!?:

                def distance_from_zero(s): if type(s) == int or type(s) == float:     print abs(s) else:     print "Nope" distance_from_zero(-410)              

points

over 8 years

Answer 5398c5ab52f863a0c6000328

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

This is my code. I just finished this, so … yeah this will give the right code. DONT forget to indent the if, elif, and else statements

points

Submitted by Soul

over 8 years

Answer 539ed0a18c1ccc15d300379a

def distance_from_zero(x):

                if type(x)==int or type(x)==float:     return abs(x) else:     return "Nope"              

points

over 8 years

Answer 53a0e8b28c1cccc254000590

It took me a long time but I got it!

def distance_from_zero(n): if type(n) == int or type(n) == float: absolute == abs(n) return abs(n)

                else:     return 'Nope'              

distance_from_zero(-10)

points

over 8 years

Answer 53a9f5fc631fe9156a00098b

This works :D def distance_from_zero(f): if type(f) == int or type(f) == float: return abs(f) else: return "Nope"

points

over 8 years

Answer 53b467c48c1ccc05970004ca

100% working:

def distance_from_zero(s): if type(s) == int or type(s) == float: return abs(s) else: return 'Nope' distance_from_zero(-10)

points

over 8 years

Answer 53e80ebd548c358ff5005192

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

points

about 8 years

Answer 53e9681852f863e0ee000482

Help

I've tried several variations on the code, including versions that people are saying is working here (like the one below):

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

distancefromzero(-5)

But it wont let me pass. It says: Your function seems to fail on input True when it returned '1' instead of 'Nope'

points

about 8 years

Answer 53f3d317631fe9e1890002fb

def distance_from_zero(s): if type(s) == int or type(s) == float: return abs(s) else: return 'Nope' distance_from_zero(-10)

points

about 8 years

Answer 53f8f29f548c35d565005227

this works correctly

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

points

about 8 years

Answer 53fb9e319c4e9dee15000021

Here is another to enter the code it works, but sorry for the indentation:

def distance_de_zero (n): print type(n) if type (n) == int or type (n) == float: le_type=True else: le_type=False if le_type is True: return abs(n) if le_type is False: return "Ce n'est ni un int ni un float !" distance_de_zero (-20)

points

about 8 years

Answer 544f998780ff335b1a001926

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

(Include indentation to work)

points

almost 8 years

Answer 5451608a282ae35360000708

IF u wanna skip this just copy paste this code.

def distance_from_zero(z):

if type(z) == int or type(z) == float:
return abs(z) else: return "Nope"

points

almost 8 years

Answer 5452b636282ae378970000df

THIS CODE WORKS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! There have been lots of people saying stuff like 'oh this code works' but when I tried it I kept on getting errors however I used this code and it accepted it. So if you do get an error you probably didn't enter this one in properly. MAKE SURE YOU INDENT PROPERLY, if you use copy and paste you will have to sort the indentions out. def distance_from_zero(a): if type(a) == int or type(a) == float: return abs(a) else: return 'Nope' distance_from_zero(-5) You only need to copy and paste the bold bit.

points

almost 8 years

Answer 546383257c82ca70f5003356

Did I miss a lesson here or something? This looks like nothing I have seen in this section

points

almost 8 years

Answer 54649e5980ff3369500028ac

This Worked for me and solved the global error issue and the indentation error. Hope this helps solve your problems.

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

points

almost 8 years

Answer 54650aa37c82ca6ede00013c

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

This is work. The program does not like how you enter the code, not what. The same code without spaces and tab throws an error. Try to remove the same code tab )

points

almost 8 years

Answer 547f2ad8282ae35d1f00214d

points

almost 8 years

Answer 5481b03dd3292f1ce9000b14

def distance_from_zero(a): if type(a) == int or type(a) == float: return abs(a) else: return 'Nao'

print distance_from_zero(a)

max(2,3,4) #4 min(2,3,4) #2

abs(2) abs(-2)

points

almost 8 years

Answer 54861235d3292ffe520009db

Just a heads up for those who are confused like I was. I used the code below and got the error "Your function seems to fail on input True when it returned 'nope' instead of 'Nope'". Turns out the site doesn't like nope to be all lower case. Change the line* return "nope" to return "Nope" *and it accepts it.

def distance_from_zero(dfz):

                if type(dfz) == int or type(dfz) == float:     return abs(dfz)   else:     return "nope"              

points

almost 8 years

Answer 548c6ef7d3292fc9ad004b09

I did this one. Hope it helps…

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

points

almost 8 years

Answer 548f1a929113cb72380092ba

Please give me the right answer.

points

almost 8 years

Answer 5494deb076b8feae040027d0

Hi, in the french version of this exercise, the text proposes to write a function named distance_de_zero, but the compiler expects a function named distance_from_zero..

points

almost 8 years

Answer 549db82695e378f7da00ed71

Why isn't my code working?

                def distance_from_zero(a):     if type (a) == int or type(a) == float:         print abs(a)     else:         print "Nope" distance_from_zero(-10)              

It keep giving me the "Your function seems to fail on input -10 when it returned 'None' instead of '10'" error.

points

almost 8 years

Answer 54a059a086f552877c01175f

i dont understand what int and float is xDD

points

almost 8 years

Answer 54d3347251b887d31a002452

If none of these work, this does :) def distance_from_zero(s): if type(s) == int or type(s) == float: return abs(s) else: return 'Nope' distance_from_zero(-10)

points

over 7 years

Answer 54dcccf676b8fe9a0000198b

Far more concise ; if type(p) in (int,float): ….

points

over 7 years

Answer 54e6836a86f5524b4b000130

thank you, too! I was wondering about that! I made the mistake, and I had no idea what was going on until I checked the hints.

points

over 7 years

Answer 550c9a2e51b88795f3000ee0

Help???? Why doesn't this work? It looks like it has worked for other people

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

points

over 7 years

Answer 55142ff895e3789868000519

points

over 7 years

Answer 5539568176b8fe6ea7000547

I found a easier way to write this:

                def distance_from_zero(s):     if type(s) in (int, float):         return abs(s)     return "Nope"              

Just use type(s) in (int, float), it should work for you :).

points

over 7 years

Answer 553fb0d651b8875cfe000070

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

points

over 7 years

Answer 554105b786f552563f000452

that's work:

                def distance_de_zero(p):      if  str(p)=="True" or str(p)=="False":         return "Ce n'est ni un int ni un float !"             else:         if type(p)==float or type(p)==int:         return abs(p)     else:         return "Ce n'est ni un int ni un float !"              

points

Submitted by S711

over 7 years

Answer 556367c09376769e0a000215

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

WORKS Fine! :) With me

points

over 7 years

Answer 55ad64f79113cb03650002a5

Had all the problems as stated in this topic. Finally this one worked:

def distance_from_zero(a): if type(a) == int or type(a) == float: return abs(a) else: return 'Nope'

distance_from_zero(-10)

points

over 7 years

Answer 55ae638176b8fe20170000a9

points

Submitted by devg

over 7 years

Answer 55ecc9a89113cbd6f700049b

this code works : def distance_from_zero(a): if type(a) == int or type(a) == float: return abs(a) else: return "Nope"

points

about 7 years

Answer 51c8850f52f8637bee014a1e

Thanks, I was hoping to this find post when I clicked forum!

points

Submitted by nid

over 9 years

Answer 53055a8952f863da8d00164b

                def distance_from_zero(b):     c = type(b)     if c == int or c == float:         return abs(b)     else:         return "Not an integer or float!"              

points

over 8 years

Answer 537f4594282ae3b95f002862

Hi.. If anyone is having trouble with 19/19, this is the correct code:

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

distance_from_zero(-5)

points

over 8 years

Answer 53b1473e8c1ccc37b000487c

points

over 8 years

Answer 5200bdc6548c35658b0029c2

I use the substring method,here is my code.

                def distance_from_zero(p): s=str(type(p))       """type function returns the 'type' datatype,using the str() function to convert.  if s[7:10]=="int" or s[7:12]=="float":     return abs(p) else:     return "Not an integer or float!"              

points

about 9 years

Answer 5255180e548c3568c8001662

I was getting a unicode error until I abandoned the "or" logic in favor of "in(x,y)"

This is what worked for me.

                def distance_from_zero(s):     if type(s) in (int, float)         return abs(s)     else:         return 'Not an integer or float!'  print distance_from_zero(                     raw_input(                     'enter integer or float:'))              

points

Submitted by Adam

about 9 years

Answer 518c389ef680ae55810013cb

Hey Michael, where can one find information on if-in?

points

over 9 years

Answer 52fb1aa6282ae3483000117d

it works!

                def distance_from_zero(value):     if type(value) in (int, float):         abs_value = abs(value)         return(abs_value)     else:         return "Not an integer or float!"              

points

over 8 years

Answer 51dab67b8c1ccc26dc04a8bb

def distance_from_zero(num): if type(num) == int or type(num)==float: return abs(num) else: return "Not an integer or float!"

points

over 9 years

Answer 5379bebd80ff33b235005dba

Hi everyone, If anyone is having trouble with 19/19, this is the correct code. I just figured it out!

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

distance_from_zero(-10)

points

over 8 years

Answer 51b5162552f8630e5301d9a9

Thank you!

I accidentally flagged your post because I wanted to see why people flagged it, sorry. :P

points

over 9 years

Answer 51c89f318c1cccb5bc0011bf

def distance_from_zero(p): if type(p) == int or type(p) == float: return abs(distance_from_zero(p)) else: return("Not an integer or float!")

points

over 9 years

Answer 518fd8021dab4010080031c0

points

over 9 years

Answer 51b44459631fe98e010167b6

points

over 9 years

edmondstharne.blogspot.com

Source: https://www.codecademy.com/forum_questions/5187c9af569b6ae7ab004fae

0 Response to "Python Check if Value is Float Continue if So"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel