Search code examples
pythonlistrandomvarnameerror

NameError: name 'x' is not defined Python


I'm trying to get around this error but can't find a solution. Actually the code worked perfectly some time but suddendly started to drop the error:

"NameError: name 'number_exit' is not defined" 

And also:

"NameError: name 'price_buy' is not defined"

The code is generating a list of random numbers

import numpy as np

# This function generates a list of numbers under certain rules
def num_var_list():
    global number_list, number_exit, number_target
    number_list = []
    number_target = number_exit
    num_max_robot = (number_target * 20) / 100
    while num_max_robot > 0:
        num_robot = np.random.randint(1,int(num_max_robot))
        if num_robot > number_target:
                number_list.append(number_target)
        else: 
            number_list.append(num_robot)
        number_target = number_target - number_target
    return number_list
        
# This function generates a random number between a certain range
def fun_price_buy():
    global price_buy
    price_buy = np.random.randint(50000,300000)
    return price_buy

# This function generates a random number between a certain range
def fun_mx_buy():
    global number_exit
    number_exit = np.random.randint(50, 150)
    return number_exit

lista_number_list = []
lista_price_buy = []
lista_mx_buy = []

# This loop append each function 50 times to a new list
while len(lista_price_buy) <= 50: 
    lista_number_list.append(num_var_list())
    lista_price_buy.append(fun_price_buy())
    lista_mx_buy.append(fun_mx_buy())

Actually, when Python doesn't drop the error, the code makes exactly what I want it to do. So I'm not sure how to adjust it to let it work without NameError warnings.

Any help will be highly appreciated. Thank you!


Solution

  • When doing global price_buy that means you use the globally defined price_buy to be defined localy in your method but

    • neither price_buy nor number_exit are defined globally (outside a method)
    • you don't need global variable

    They only to be local, and just better : inlined

    def fun_price_buy():
        price_buy = np.random.randint(50000,300000)
        return price_buy
    
    # inline, no temporaty variable is needed
    def fun_price_buy():
        return np.random.randint(50000,300000)
    

    Finally, and if you want to get the value from the method in a variable for doing something with it:

    import numpy as np
    
    # This function generates a list of numbers under certain rules
    def num_var_list(number_exit):
        number_list = []
        number_target = number_exit
        num_max_robot = (number_target * 20) / 100
        while num_max_robot > 0:
            num_robot = np.random.randint(1,int(num_max_robot))
            if num_robot > number_target:
                    number_list.append(number_target)
            else: 
                number_list.append(num_robot)
            number_target = number_target - number_target
        return number_list
            
    def fun_price_buy():
        return np.random.randint(50000,300000)
    
    def fun_mx_buy():
        return np.random.randint(50, 150)
    
    lista_number_list = []
    lista_price_buy = []
    lista_mx_buy = []
    
    # This loop append each function 50 times to a new list
    while len(lista_price_buy) <= 50: 
        number_exit = fun_mx_buy()
        price_buy = fun_price_buy()
        vr_list = num_var_list(number_exit)
       
        lista_number_list.append(vr_list)
        lista_price_buy.append(price_buy )
        lista_mx_buy.append(number_exit )