function undefined error in python

With this explanation, we will understand that it can happen when a Python program shows an error like NameError: name '' is not defined, even if the function exists in the script.

We also learned what happens when we use misspelled variables or built-in functions that are not imported, and how to avoid these mistakes in Python.

Avoid calling functions before Python declarations

There are many reasons for NameError: function is not defined in Python, we will discuss the most common reasons for this error. When we call a function before it is defined in the program, the Python interpreter will not be able to find the function definition.

In this case, the function will not be visible, the Python interpreter has not yet encountered the function definition. So the Python interpreter gets confused and throws an error, which means that the function has not been defined according to the interpreter.

Let's see a simple example. In this program, we write a call statement before defining the function. Since the interpreter executes the Python program line by line, when it starts executing the first line, it encounters a specific function call, but it doesn't know what Hello() is.

Hello()
def Hello():
    print('I will never be called')

When we run this Python script, it gets confused whether it's a function, and the Python interpreter stops and throws an error. This is because the definition of this function comes after the function call; that's why we can never call a function before it is defined.

NameError: name 'Hello' is not defined

We need to define it before calling the function to fix this error.
Avoid misspelled variable or function names in Python

Another reason for this error is that the user makes a mistake in defining the correct spelling of the function; that's why the user gets this type of error. Python is case sensitive, so lowercase and uppercase will be different functions.

We examine an example to demonstrate how the interpreter behaves when it gets a misspelled variable.

Orange='orange'
for i in orange:
    print(i)

output:

NameError: name 'orange' is not defined

When we run the Python script, we get an error saying 'orange' is not defined, but we didn't make a typo this time, so why do we get this error even though we defined the variable? This is because we defined a variable starting with a capital letter and tried to access it with a lowercase letter.

The same scenario would apply to functions. If we override the orange variable with Orange it works.

orange='orange'
for i in orange:
    print(i)

output:

o
r
a
n
g
e

Avoid using built-in modules without imports in Python

For example, when you are writing a Python program, if you want to use any built-in functions like print(), input(), etc., you can use it in your code. What happens when you need to use a function that is not built into Python, but exists in some module?

To use functions belonging to some module, that module must first be imported into a Python program. Some beginners fail when trying to use a function in a module without importing that particular module; see an example.

In this program, the user is trying to generate random values, but when the user runs this code, the user gets the same error we discussed. The reason is that the user fails, it tries to access the random() function without importing its module random.

Randome_Values=random.random()
print(Randome_Values)

output:

NameError: name 'random' is not defined

It works when the user imports the random module in a Python script.

import random
Randome_Values=random.random()
print(Randome_Values)

output:

0.07463088966802744

Sometimes, users write Python modules themselves, but run into the same problem, and get the same error.

This is because the user tried to call a function from another file after importing the class, but maybe the user didn't save the file where the function was defined, that's why the user got this error. So make sure to save it before calling this function.

Fix variable out of range issue in Python

Another reason most beginners fail is that they get the same error when they try to access variables that are out of scope. Let's see an example and we will get a better understanding.

def take_order():
    orders=input("Enter your orders and separate it using comma :").split(',')
    return orders
def Delete_Orders():
    deleted_orders=input("Enter your order name which you want to cancel :")
    orders.remove(deleted_orders)
    return orders
print(take_order())
print(Delete_Orders())

When we run this Python script, the take_order() function will work fine, but when we call the Delete_Orders() function to delete orders from the orders list, an error occurs.

NameError: name 'orders' is not defined

This is because the orders variable is defined in the take_order() function and we are trying to access it from the Delete_Orders() function. That's why when execution control reaches the point where we remove the item from the orders list; it throws an error because we are accessing it outside of its scope.

To fix this, we have to declare a variable called orders that stores an empty string. After that, we need to use the global keyword, which we call the orders variable, to make it reusable.

orders=''
def take_order():
    global orders
    orders=input("Enter your orders and separate it using comma :").split(',')
    return orders
def Delete_Orders():
    deleted_orders=input("Enter your order name which you want to cancel :")
    orders.remove(deleted_orders)
    return orders
#Python小白学习交流群:711312441   
print(take_order())
print(Delete_Orders())

output:

Enter your orders and separate it using comma :pizza,drink,water
['pizza', 'drink', 'water']
Enter your order name which you want to cancel :water
['pizza', 'drink']

Guess you like

Origin blog.csdn.net/qdPython/article/details/132121027