-1

I have 2 functions fun_1 and fun_2

fun_1 → loops over fun_2 and stores the output of it in a variable named X.

I want the return value of fun_1 to be the value of each iteration of the loop, so the return value I need is a tuple of 5 values, each value for X in one of the iteration instead of the value of only the last iteration.

Note:fun_1 and fun_2 are just examples for functions just to demonstrate the idea and understand it so that I can apply it to complex function.

def fun_1():
    N = 0
    while N <= 5:
        X = fun_2()
        N = N+1
    return(X)
martineau
  • 119,623
  • 25
  • 170
  • 301
Mosalah
  • 5
  • 3

2 Answers2

1

Have you considered utilizing a list? For example:

def fun_1():
    N = 0
    X = []
    while N <= 5: 
        X += [fun_2()] # or X.append(fun_2())   
        N += 1
    return X
1

like the below?

def fun_2():
    return 9
    
def fun_1():
    return tuple(fun_2() for i in range(0,5))
    
print(fun_1())

output

(9, 9, 9, 9, 9)
balderman
  • 22,927
  • 7
  • 34
  • 52