Review: Built-In Functions


#1

I am a little bit confused why my answer does not work. I tried it directly on my python setup and it works just fine.

This is what I came up with:

#!/usr/bin/python
def distance_from_zero(number):
        if type(number) == str:
                return "Nope"
        else:
                return abs(number)

test=raw_input("Give me something to check: ")
print distance_from_zero(test)

This is what made me pass that exercise:

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

#2

I should have said that this is for the python course.


#3

Since there are multiple data types, it is simpler to test for legal types than illegal types so it makes sense to test for number types.

def distance_from_zero(arg):
    if type(arg) == int:
        return abs(arg)
    elif type(arg) == float:
        return abs(arg)
    else:
        return "This isn't an integer or a float!"

That is why your second example passes.


#4

Consider the following...

def distance_from_zero(number):
    if type(number) == str:
        return "Nope"
    else:
        return abs(number)

Can we be sure that number is not a Boolean? The else statement makes a dangerous assumption.


#5

I did not consider Boolean as an input. This makes sense! thx!


#6

Hi @pieslinger ,

Something to keep in mind about raw_input is that it always returns a str type, even if the user enters all digits or the characters True. What may look like a number or a bool on the console is actually input as a string.

So this ...

test=raw_input("Give me something to check: ")

... will assign a str to test. Then, when you call the distance_from_zero function, with test as an argument, the parameter, arg, will represent a str, and the function will return "Nope", even if it looks like you entered a number.


#7

'input' in this instance refers to 'parameter', not user input. It's what is passed to a function.


#9

i fixed mtfs work and it works
def distance_from_zero(arg):
if type(arg) == int:
return abs(arg)
elif type(arg) == float:
return abs(arg)
else:
return 'Nope'


#10

Hi man your code is completely right . i wrote this and i don't know why my answer is not true can u help me

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

#11

Take care with indentation in Python. It has meaning that is critical to scoping.

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

#13

well, i have no prob with my code all i know is that i wanna try to write this in real form, i mean i wanna ask a client to import somthing and then find out if the resault is true or false
here's my code:

test_my_code=input("please import number or text here:")


def distance_from_zero(n):

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

    else:
        return "Nope"

when i test this in paycharm, the resault is blank! i can't find the prob


#14

Did you write a call expression?

distance_from_zero(test_my_code)

Keep in mind that this site is still using 2.7 not 3.x. raw_input might better serve your needs, at least here. Pycharm is probably 3.x so there will be some differences from this one to that.


#15

tanks guys that was really helpfull