. Advertisement .
..3..
. Advertisement .
..4..
When you are starting with Python programming, the NameErrors are quite intimidating which keeps you worried and searching for days to get through it. The NameError: function is not defined in Python is the type of error that doesn’t allow you to get the result you are looking to obtain from the program.
The NameError: function is not defined in Python occurs when you use a function that is not valid. Let’s have a look at how to fix this error
How to Fix NameError: function is not defined in Python
This error warning shows up when you are trying to call a function that is not declared or either declared before. Check out the example of how you get this error
print(do_math(1995, 1987))
def do_math(x, y):
return x + y
Traceback (most recent call last):
File "code.py", line 1, in <module>
print(do_math(1995, 1987))
NameError: name 'do_math' is not defined
The reason for the error is that the function is not defined and we called it anyway.
Declare the Function
First, you have to declare the function before calling it as it is one of the most effective solutions that work. Check the example
def do_math(x, y):
return x + y
print(do_math(1995, 1987))
Output
3982
Avoid Misspelling Function Name
Names are case-sensitive, and if you misspell the function, then you can end up with the NameError.
Code:
def do_math(x, y):
return x + y
print(do_Math(1995, 1987))
Traceback (most recent call last):
File "code.py", line 3, in <module>
print(do_Math(1995, 1987))
NameError: name 'do_Math' is not defined. Did you mean: 'do_math'?
The do-Math function has a capital ‘M’ that returns an error.
Define Function
Another reason to have a NameError is when you try to use a built-in module and forget to import it. Have a look at the example
Code:
print(math.floor(1989.2015))
Error message
Traceback (most recent call last):
File "code.py", line 3, in <module>
print(math.floor(1989.2015))
NameError: name 'math' is not defined
Here, the ‘math’ module is used but without importing it. Python has no idea what is ‘math’ if you don’t define the function.
Solution
import math
print(math.floor(1989.2015))
Output
1989
Don’t call scoped Function from Outside
This also causes an error if trying to call a function this is scoped from outside. The ‘outer’ function has an ‘inner’ function inside of it, so it shouldn’t be called from outer space. Check an example
def outer():
def inner():
return 'NameError: function is not defined in Python'
print('success')
outer()
inner()
Solution
The easiest way to get through the error is to declare the function. The function should be declared from outer space if you have to access it from outside
def inner():
return 'NameError: function is not defined in Python'
def outer():
print(inner())
outer()
inner()
To get the result from the ‘outer()
’ function by calling the ‘inner
’ function, see the example
def outer():
def inner():
return 'NameError: function is not defined in Python'
result = inner()
return result
print(outer())
Output
NameError: function is not defined in Python
Conclusion
And here we are! We discussed all the possible ways that can cause NameError: function is not defined in Python. I hope you can solve this NameError and continue with the programming!
I wish you the best of luck!
Do comment below if you are stuck somewhere!
Leave a comment