Py学习  »  Python

NameError:未在Python中定义名称“x”

Foldhy • 2 年前 • 999 次点击  

我试图避开这个错误,但找不到解决办法。事实上,代码在一段时间内运行良好,但突然开始删除错误:

"NameError: name 'number_exit' is not defined" 

而且:

"NameError: name 'price_buy' is not defined"

代码正在生成一个随机数列表

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())

实际上,什么时候 Python 不会删除错误,代码完全符合我的要求。所以我不知道如何调整它,让它在没有名称错误警告的情况下工作。

任何帮助都将不胜感激。非常感谢。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/130451
 
999 次点击  
文章 [ 5 ]  |  最新文章 2 年前