Home > Net >  How to store outcome of for loop in a variable in python
How to store outcome of for loop in a variable in python

Time:05-01

I have a function within which I have a for loop that is looking for a certain string, which if true executes a different function. What I can't work out is how to create a variable within which to store the outcome of the for loop. I want to take the outcome (i.e. this function was executed or this string was present) and pass that into another function but I cannot get the variable to remember the outcome. Any help would be greatly greatly appreciated. Thank you

for something in something:
    def a_func():
        
        if this  == 'A':
            func_1()
        if this  == 'B':
            func_2()    
        if this  == 'C':
            func_3()  
        if this  == 'D':
            func_4() 

        return this

    this = a_func()

CodePudding user response:

What you want to do and what you have done do not seem to align well. Currently, your code is poorly written. However, it can be fixed as follows:

def func_1():
    return 1
    
def func_2():
    return 2
    
def func_3():
    return 3
    
def func_4():
    return 4
    
action_for_value = {
    'A': func_1,
    'B': func_2,
    'C': func_3,
    'D': func_4,
}
    
for something in ['A', 'B', 'C', 'D', 'E']:
    print(action_for_value[something]() if something in action_for_value else "Not found!!")

This example stores functions corresponding to values in a dictionary and then basis if the value is present, makes a function call.

Here it is in action.

Some notes about your current code:

  1. Declaring a function inside a for loop is never correct, and has quite a few consequences. Move it outside the for block, and then call the function inside the for block.
  2. You want to assign the value of the func_x to a variable which is either global or at least outside the for loop. Alternatively, you can return from a_func the value from func_x as if this == 'A': return func_1() and then use the return value to assign this.

From the looks of it, I would ask you to review your understanding of the basics of Python. A lot of it seems cobbled together without a good understanding of what is happening.

CodePudding user response:

I'm not sure what you're actually trying to do, but defining a function inside of a for loop is definitely not the way to do so.

def func_1():
    pass

def func_2():
    pass

def func_3():
    pass

def func_4():
    pass

def a_func(this):
    if this  == 'A':
        func_1()
        return f'{this} found and func_1() ran.'
    elif this  == 'B':
        func_2()    
        return f'{this} found and func_2() ran.'
    elif this  == 'C':
        func_3()  
        return f'{this} found and func_3() ran.'
    elif this  == 'D':
        func_4() 
        return f'{this} found and func_4() ran.'
    else:
        pass
        

something = ['A', 'B', 'C', 'D']

outputs = [a_func(x) for x in something]

for value in outputs:
    print(value)

Output:

A found and func_1() ran.
B found and func_2() ran.
C found and func_3() ran.
D found and func_4() ran.
  • Related