Review:built-in functions - Stuck, help please!


#1



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


I think my code is correct, but I am receiving this error!
Oops, try again. It looks like you have a NameError. Maybe you forgot to create a function called distance_from_zero? Check the error message for more info! - global name 'distance_from_zero' is not defined


def distance_from_zero(value):
    return()

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

distance_from_zero(30)


#2

Make sure the if else block of code is indented into your function.


#3

Hello @giganinja49132
You code have two main problems.

1.return() -- In the exercise, is there mention of this thing? is it important? (I think not!)

2.This block of code should be inside function and indented by 4 spaces.

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

#4

Yes...now it got executed.
Thank you!
I didn't notice the indentation and I thought we need that return() for the output


#5

There is nothing like return() in python, return is a keyword that simply used to hand over some value to the place where the function gets called.

When function reaches it (inside function's body),it halts the execution.

for example..

def someFunction():
    return something

Also return only makes sense inside the function.


#6

Sorry to ask this!! I am new to Python and I wanted to know..
So you mean, even though we type the return statement it doesn't print anything.
It just takes the output of the function!?


#7

no, return does not print something when we call the function,It just hands over the value.We can actually save it in variable then print it or can directly print by passing function call to print function.
Like below example..

def someFunction():
    return 5

someFunction() #This will not print anything
print someFunction() #It will print 5

# or one thing we can do is to catch the value in a variable
# then print it

someVariable = someFunction()
print someVariable #It will print 5

#8

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