Home > Software design >  How to reuse a function within the same python script
How to reuse a function within the same python script

Time:12-15

I am writing a program and I would like to call twice the same function but with different parameter values.

def finding_numbers(x, y, z):
    """some code here"""
    return z

def one():
    """some code here"""
    return

def try_again(e,t,q):
    p = finding_numbers(x=e,y=t,z=q)
    return p

def main():
    finding_numbers(x,y,z)
    one()
    try_again(e,t,q)

main()

I have tried to call the function as the code above but I don't get the expected return, in fact, it returns nothing. I have tried to create a function with different name def try_again(finding_numbers(x=e,y=t,z=q)) but it does not work. I have also tried to call it from the main again as finding_numbers(x=e,y=t,z=q). I have been ready about how to re-use function within the same python script and I cannot find anything suitable. How to process this?

CodePudding user response:

For your example, your 'try_again' function is not written according to your description.

def try_again(e, t, q):
    q = finding_numbers(x=e, y=t, z=q)
    return q

CodePudding user response:

you can call the function finding_numbers inside try_again, instead of passing it in as a parameter:

def try_again(e, t, q):
    return finding_numbers(e, t, q)
  • Related