19. Review: Built-In Functions


#1



19. Review: Built-In Functions

Oops, try again.
Your function seems to fail on input -10 when it returned '25' instead of '10'


What's the problem, guys?


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


#2

why did set number to 25 , so every time you run this code , it will return 25 !!!! even if we enter distance_from_zero(1000)
also i think your condition is not correct !


#3

hmm, logic is kinda strange, you are saying if 25 is not int or 25 is not float return abs.

instructions said if 25 is int or float return abs
If the type of the argument is either int or float, the function should return the absolute value of the function input.


#4

Your code on line 3 should be == int NOT != and the same with != float. What you're saying there is if type(number) is not int, return an absolute number. Hope this helps :wink:


#6

Just help with code, please


#7

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

it's right, thanks


#8

Need help on this

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


#9

You dont need this number = 56

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

Once you complete the function you can run it with any number you like even 56.


#10

I can't comprehend what's happening in this code to be honest, and I'm afraid of going further since I haven't really learned anything :cry:


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


it said in the instructions that if argument s is either a float or an integer, that it should return the absolute value of that argument, right? If someone can explain what the if row of the code meant in English, that would be awesome!

How can it be that: if whatever type s is, is equal to a type integer or whatever type s happens to be, equals to type float?


#11

exactly! The way I comprehended the if row was like this: IF the type(argument) is equal to an integer or whatever type(argument happens to be, equals to float?


#12

do you understand what the abs() function does? This is like the main componentof this function. what you are trying to do here is find the abs (absolute value) of a float i.e (1.0,2.0.....) or an int i.e (1, 2...) and by returning the absolute value of that number you will thus know thedistance_from_zero whether the number is positive or negative. :slight_smile:


#13

yes thank you it worked


#14

That worked thanks heaps bandit


#15

I had a query but ive solved it now


#16

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

Guys someone please help and tell me as to where the code is wrong ..
it keeps on throwing the following error -

Oops, try again.
Your function seems to fail on input True when it returned '1' instead of 'Nope'

#17

You're welcome! :grin:


#18

try removing number=25. By doing that, number can take on any arbitrary value.
def distance_from_zero(number):
if(type(number)==int or type(number)==float):
return abs(number)
else:
return "Nope"


#19

Your condition is wrong and also, you set number to 25. So, each time you run the code, it checks the type of the number 25 and returns the abs of 25 which is 25. The condition should check if the type of number is int or float. Like this:

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

#20

Looks like it might be a glitch. I had the same issue and reset my code and typed the same thing again and it worked.


#21

Yes actually it might be a glitch cause this was a long time ago and I too
did the same thing and it worked. If any moderator is reading this post
please do take care and see whether this one truly has a glitch..

Ronit Gaikwad