python-3.x 遍历list,为每一项创建新的list,然后将新的list作为参数传递给函数,但是“var referenced before assignment”[重复]

ds97pgxw  于 2023-04-22  发布在  Python
关注(0)|答案(2)|浏览(124)

此问题已在此处有答案

How can a name be "unbound" in Python? What code can cause an UnboundLocalError?(3个答案)
9天前关闭
我有一个包含4个项目(用户ID)的列表。我想遍历这个列表,并将每个用户ID传递给一个函数。这个函数将为每个用户向API发出请求,返回json响应并创建列表,然后为每个用户的个人数据创建一个列表。这四个新列表将被传递给一个新函数。
但是,我得到了错误UnboundLocalError: local variable 'userid2_list' referenced before assignment
如何确保迭代完成,并在调用函数之前为每个用户id生成一个列表?

userid_list = [1, 2, 3, 4]

for userid in userid_list:
    get_info(userid)

###

def get_info(userid):
    response = requests.get(f"www.example.com/api/{userid}")
    data_list = list(response.json()['a']['b'])
    if userid == 1:
        userid1_list = data_list
    elif userid == 2:
        userid2_list = data_list
    elif userid == 3:
        userid3_list = data_list
    if userid == 4:
        userid4_list = data_list

    use_info(userid1_list, userid2_list, userid3_list, userid4_list)
vbopmzt1

vbopmzt11#

要删 debugging 误消息,请执行以下操作:
“UnboundLocalError:赋值前引用的局部变量“userid2_list”。
你必须首先初始化你的user#_list变量。你可以这样做:

# Initialize variables. You can initialize to what you want. I chose None
userid1_list = userid2_list = userid3_list = userid4_list = None

为了确保在调用函数之前完成迭代并为每个用户id生成一个列表,您可以使用用户id列表而不是单个用户id来调用get_info函数:

userid_list = [1, 2, 3, 4]
# Call get info with userid_list
get_info(userid_list)

def get_info(userids):
    # Initialize variables. You can initialize to what you want. I chose None
    userid1_list = userid2_list = userid3_list = userid4_list = None
    # Iterate through all the userids
    for userid in userids:
        response = requests.get(f'www.example.com/api/{userid}')
        data_list = list(response.json()['a']['b'])
        if userid == 1
            userid1_list = data_list
        elif userid == 2
            userid2_list = data_list
        elif userid == 3
            userid3_list = data_list
        if userid == 4
            userid4_list = data_list

    # Call user_info once all userids have been processed
    use_info(userid1_list, userid2_list, userid3_list, userid4_list)
ryevplcw

ryevplcw2#

错误“UnboundLocalError:局部变量'userid2_list' referenced before assignment”正在发生,因为您正在尝试在get_info中分配变量userid1_list、userid2_list、userid3_list和userid4_list之前引用它们()函数。发生这种情况是因为您基于userid值在“if”语句中有条件地分配这些变量,并且如果userid不匹配任何条件,这些变量将不会被赋值,从而在以后尝试引用它们时导致错误。
为了解决这个问题,你可以在if语句之前用空列表初始化这些变量,这样即使不满足条件,它们也总是有一个值。代码如下:

userid_list = [1, 2, 3, 4]

# Initialize empty lists outside of the function
userid1_list = []
userid2_list = []
userid3_list = []
userid4_list = []

def get_info(userid):
    # Use global keyword to indicate that these variables are global
    global userid1_list
    global userid2_list
    global userid3_list
    global userid4_list

    response = requests.get(f'www.example.com/api/{userid}')
    data_list = list(response.json()['a']['b'])
    if userid == 1:
        userid1_list = data_list
    elif userid == 2:
        userid2_list = data_list
    elif userid == 3:
        userid3_list = data_list
    elif userid == 4:
        userid4_list = data_list

    use_info(userid1_list, userid2_list, userid3_list, userid4_list)

for userid in userid_list:
    get_info(userid)

相关问题