Home > Blockchain >  How to pass some initial value in the first iteration of the loop and then pass the derived values i
How to pass some initial value in the first iteration of the loop and then pass the derived values i

Time:08-06

initial_state = 40

def funct_1(x):
    return y

def funct_2(y):
    return z

def main():
    funct_1(x)
    funct_2(y)

for i in range(10):
    main()

In the first iteration, I want input to funct1 i.e. x = initial_state and from the second iteration onwards, I want output of funct_2 i.e. z becomes input to funct_1. Please let me know how can I implement this in python.

CodePudding user response:

Normally, this should work:

initial_state = 40

def funct_1(x):
    return y

def funct_2(y):
    return z

def main():
    for i in range(10):
        if i == 1:
            funct_1(x)
        else:
            funct_2(y)

if __name__ == '__main__':
    main()

CodePudding user response:

Straightforward way is following:

def funct_1(x):
    y = x   1
    return y


def funct_2(y):
    z = y   1
    return z


def main(x):
    y = funct_1(x)
    z = funct_2(y)
    return z


state = 40
for i in range(10):
    state = main(state)
    

CodePudding user response:

This should work: In the first iteration you have your default value of x and in all iterations after that x is the output of funct_2().

x = init_state
def main():
   y = funct_1(x)
   x = funct_2(y)

for i in range(10):
   main()
  • Related