# How can I test my code in 19/19?

#1

def distance_from_zero(balls):
if type(balls) == int or type(balls) == float:
return abs(balls)
else:
return “Nope”

It returns me OK (no errors), but how can I see the actual mech of the code?
I want to run it with some arguments of the balls in the console.

Thank you.

#2

arguments are supplied at function call, so you could add function calls to see your code in action. Remember, functions only execute when called.

#3

Thank you for the quick answer.
I tried to call the function after defining it:

distance_from_zero(balls)

and was prompted that balls is not defined

So I put it once more this way:

balls = raw_input("Input your number: ")
distance_from_zero(balls)

print distance_from_zero(balls)

What is the problem here?

#4

its important to understand the difference between parameter (`balls` in your case), which is used when we declare the function

and the argument at function call, which can be anything which is valid and defined. In your case, this could simple be an integer

a parameter is only a placeholder, the parameter gets its value from the argument at function call.

#5

So we are calling functions with argument?
Is this the right way?

def distance_from_zero(balls):
if type(balls) == int or type(balls) == float:
return abs(balls)
else:
return “Nope”

distance_from_zero(42)
print distance_from_zero(42)

#6

if the function has a parameter, yes. That looks right, but the first of the two function calls you do nothing with the returned result

#7

Didn’t get it.

distance_from_zero(42) calls the function
and
print distance_from_zero(42) prints the result in the console

that is how I understand it. Am I not right?

#8

yes, it does. Then the function returns a result, but you do nothing with the returned result?

at the second function call, you print the returned result, so at least you do something with the result

#9

Got you.

return distance_from_zero(42)
print distance_from_zero(42)

… No. Actually code is not working this way

#10

`return` can’t be used outside a function.

you have two function calls here:

``````distance_from_zero(42)
print distance_from_zero(42)
``````

that you understand correctly.

The function call will execute the function, the function will then return a result.

your second function call will print the returned result given you include a print statement before the function call.

the first function call will do nothing with the result returned by the function

#11

Now it becomes clear.
So I can, say, assign a result to some variable in the same calling-function line -

result = distance_from_zero(42)
print result

#12

Generalise a bit more than that.

An assignment statement has a name on the left side of `=` and an expression on the right side.
The expression is evaluated and the name is assigned to the result.
A function call is an expression.

An expression is something that evaluates to a value, for example 5 is an expression because it evaluates to 5 (itself), and 5 + 5 is an expression, it evaluates to 10. Names are expressions, a name evaluates to the value it refers to. More complicated expressions consist of several subexpressions (like 5 and 3 in 5 + 3)

…Otherwise you’ll be memorizing fifty different versions of the same thing

In contrast, a statement has no result. You can for example not assign a name to a for-loop, because a for-loop has no result, it’s not an expression.

#13

Was I incautious in definitions and using the same word (result) in two meanings?

What if I put it this way:

actual_number = distance_from_zero(42)

the string that 1) calls the function, 2) then returns the result (“evaluates it” in your words) and 3) then assigns it the name (“actual_number”).

Then goes the printing statement - that has nothing to do with assignment.

print actual_number

But I must confess I understood just a fraction of your generalization…

#14

I mean that you should not consider a function-call special.
It’s an expression. Use it wherever an expression is allowed. So it is for example allowed anywhere you could put the number 1, because that is also an expression.

#15

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