定义全局变量时的NameError

时间:2017-12-22 13:08:45

标签: python function global

我正在展示代码的一个小节,我在其中定义了两个函数defining_polygon_outside_nestdefining_polygon_inside_nesterr是一个整数,其值的范围为[0,5]。我将err定义为这些函数中的全局变量,并希望使用此函数在列表ants_inroi_outsidenestants_inroi_insidenest中添加特定值。每当我这样做时,我都会收到错误if for_coordinates_inside_nest((x,y)) is True and err==0: NameError: global name 'err' is not defined如果我在功能err中没有使用defining_polygon_inside_nest 它可以解决此问题吗?

err = 0
def defining_polygon_outside_nest(tag_data):
    global err
    ants_in_arena = []
    ants_inroi_outsidenest = []
    for id, (x,y) in tag_data:
        ants_in_arena.append(id)
        if for_coordinates_outside_nest((x,y)) is True and err==0:
            ants_inroi_outsidenest.append(id) 
    return ants_inroi_outsidenest


def defining_polygon_inside_nest(tag_data):   
    global err
    ants_in_arenai = []
    ants_inroi_insidenest = []
    for id, (x, y) in tag_data:
        ants_in_arenai.append(id)
        if for_coordinates_inside_nest((x,y)) is True and err==0:
            ants_inroi_insidenest.append(id)
    return ants_inroi_insidenest

0 个答案:

没有答案