-1

I have a list of 4 items (user ids). I want to iterate through the list and pass each user id to a function. This function would do a request to an api for each user, return json response and create list, and then create a list for each user's individual data. These four new lists would be passed to a new function.

However, I am getting the error UnboundLocalError: local variable 'userid2_list' referenced before assignment.

How can I ensure the iteration is completed and a list generated for each user id before the function is called?

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)
HeroicHitesh
  • 77
  • 1
  • 8
  • 1
    First correct your syntax. Missing colon here: `def get_info(userid)`, `response = requests.get(f'www.example.com/api/{userid}")` string begins with `'` and ends with `"` – Bibhav Apr 13 '23 at 03:43
  • Think carefully about the logic. If you want to call `use_info(userid1_list, userid2_list, userid3_list, userid4_list)`, then all of those things - `userid1_list`, `userid2_list`, `userid3_list`, `userid4_list` - need to exist first, right? Now, what happens if, for example, `userid` is equal to `1`? Will `userid2_list` get set? How? If it is not set, how can it be used? – Karl Knechtel Apr 13 '23 at 04:44

2 Answers2

-1

To get rid of the error message:

"UnboundLocalError: local variable 'userid2_list' referenced before assignment.

You have to initialize your user#_list variables first. You can do that like so:

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

To ensure the iteration is completed and a list generated for each user id before the function is called, you can call the get_info function with the list of userids instead of a single user id:

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)
Marcelo Paco
  • 2,732
  • 4
  • 9
  • 26
-1

The error "UnboundLocalError: local variable 'userid2_list' referenced before assignment" is occurring because you are trying to reference variables userid1_list, userid2_list, userid3_list, and userid4_list before they are assigned inside the get_info() function. This happens because you are assigning these variables conditionally inside "if" statements based on the userid value, and if the userid doesn't match any of the conditions, the variables won't be assigned, resulting in the error when you try to reference them later.

To fix this issue, you can initialize these variables with empty lists before the if statements so that they always have a value even if the conditions are not met. Here's the code:

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)