Why will my 'type(x) == int' check not work?


#1


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

The check for ==int seems to be bypassed no matter how I try to do it - and I'm stumped!
The code is "accepted" by the module; I am not satisfied though because each time I try running I get no print of the absolute of the entered value as I expect, and the code jumps to the <else> line and executes.


I expect the entry of -9, or 9, to give a val of 9
I expect the entry of 9f to give Nope


global v
v = ""

def distance_from_zero(v):
    if type(v) == int or type(v) == float:
        return abs(v)
        print (abs(v))
    else:
        print "Nope"
        return "Nope"
kb = raw_input()
distance_from_zero(kb)


#2

i added a line of code to your code:

kb = raw_input()
print type(kb)

run it please, now, what can we conclude from this? And in which way does this explain why your code won't work?


#3

Hi,
I get a Unicode return from that type check.
Seems I'm not grabbing the right kind of input, and need to NOT us raw_input here...


#4

the alternative is to use input instead, those are the two built in functions you have, you could check the documentation i provided to see if this fits your needs


#5

I get most of it now, thank you.
I had not noticed that Return () closes the loop, so now I've juxtaposed print and return it's giving me my answer.

However:
If I enter a string (pw for instance instead of a number) I get:
pw
Traceback (most recent call last):
File "python", line 12, in
File "", line 1, in
NameError: name 'pw' is not defined

How do I overcome this error fail?

global v
v = ""

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

#6

i don't know, did you check the documentation about input? i am not that familiar with this function, given input in python3 works as raw_input in python2

personally, i would use try/except to see if a number can be casted to a integer or float, and just use raw_input.


#7

Yes - that works.

Thank you both for your help - I was frustrated by my ineptitude. :confounded:

Could you possibly have a look at my code here and let me know if there's a markedly more elegant way of doing this test - catch an input and null it if it is NOT an int or a float.

v = ""
def go_get(V):
    try:
        v = input()
        if type(v) == float or type(v) == int:
            distance_from_zero(v)
    except:
        pass
        print "try again"
        v=""
        go_get(v)
    
            
def distance_from_zero(v):
    if type(v) == int or type(v) == float:
        print abs(v)
        return abs(v)
    else:
        print "Nope"
        return "Nope"
       
go_get(v)

#8

this code can definitely be optimized, a nice challenge for you once you progressed more into the course.

I helped you quit a lot already, how can i friendly deny this request? Nothing of course then is stopping you from make a new topic to ask about optimization.


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.