X not defined?

In [6]: def mathematics(x, y, z):
…: x = (4*5)+2
…: y = (27-18)2
…: z = 18
…: return (x * y)%z+(24
4)


NameError Traceback (most recent call last)
in
----> 1 mathematics(x, y, z)

NameError: name ‘x’ is not defined

What else are my suppose to do?

Your error message isn’t complaining about your function, is it?
It’s about a different expression that you wrote somewhere else.

mathematics(x, y, z)
That expression refers to four names, mathematics, x, y, z
In the context that you are using those names, are they defined?

So before you do something like:

a + a

You have to first do something like:

a = 1

Or, if you didn’t mean to define them, then maybe they shouldn’t be used at all either, so you would remove them from your expression.

Your function accepts three arguments but completely ignores them, so there seems to be no purpose to them.

What is the purpose of function parameters? (sending information to a function) Are you using that?

You might want to start out with a simpler function, or perhaps no function at all, and only add the things that you strictly need, and test the code each time you add something to see if it still holds together.

@sodiqolabodeafolayan, where is this from? Please provide a link.

Your function doesn’t really look like a Python function: use the </> icon for formatting:

def mathematics(x, y, z):
    x = (4*5)+2
    y = (27-18)+2
    z = 18
    return (x * y)%z+(244)
mathematics(x, y, z)
''' Output:
Traceback (most recent call last):
  File "C:\Users\patrick\scripts\test.py", line 8, in <module>
    mathematics(x, y, z)
NameError: name 'x' is not defined
'''

… shouldn’t it be:

# these two lines are the function
def mathematics(x, y, z):
    return (x * y)%z+(244)

# these are variables which will be passed to the function ..
x = (4*5)+2
y = (27-18)+2 
z = 18

# ... by means of this function call
mathematics(x, y, z)

''' Output:
252
'''

The symbols x, y and z are the parameters of the function. They tell the function:

“When this function is called by a statement such as mathematics(value_1, value_2, value_3), you are to carry out the calculations within the function body (the indented block below the def line), and, in doing so, assign value_1 to x, value_2 to y and value_3 to z (corresponding to the positions in the calling statement’s parentheses), within the function body.”

In the first example, none of this happens; x, y and z are assigned values within the function body, and when the function call comes along, mathematics(x, y, z) - outside of the function - the x, y, and z seen within it have no values attached. The error message just stops with x, and parsing stops there.

Thank you very much for pointing this out. This is the essence of practice! practice!! practice!!!. You just “put right” an idea. Thank you once again

The is highly appreciated. Thank you very much for pointing this out. This is the essence of practice! practice!! practice!!!. You just “put right” an idea. Thank you once again

To put into perspective, and based on your solution below, will it be right to say

  1. It was wrong that i declared the value of the variables/parameters (x, y, z) within the function
  2. Values of parameters should be declared outside/after the function especially when the function caries “return”

Yes, exactly.

To be precise, the value assigned to a function’s parameter is called an argument.

What an amazing summary. It becomes clearer and clearer.

Just to mention. see below what i was actually trying to do

In [47]: def my_exponent(x, y, z):
…: x = (x * 25) **2
…: y = (y * 35) **3
…: z = (z * 32) **2
…: return (x, y, z)
…:

In [48]: my_exponent(2, 3, 4)
Out[48]: (2500, 1157625, 16384)

I initially thought that giving the equation for each argument within the function will suffice as its value in the calling statement. This was wrong as you have helped in pointing out. The correct idea is that i can have equations for arguments within the function, actual value will have to be passed during the calling statement…this applies to positional argument. However, for keyword argument, i do not necessarily need to be explicit with the value while calling the statement.

Once again, thank you for your help

1 Like