vendredi 2 avril 2021

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!




Aucun commentaire:

Enregistrer un commentaire