-1

I want do take my function

def function_name(inputa, inputb)
    #long formula which uses 2 inputs
    return answer

if __name__ == '__main__':
    function_name(float(sys.argv[1]), float(sys.argv[2]))

input to bash

 python3 function_name.py 0.3 0.2 0.1 0.5

would like to take inputa, inputb = 0.3, 0.2 and run formula for answer 1 then rerun formula with inputa, inputb = 0.1, 0.5 and give the output for answer 2

this should work with n amount of inputs such as python3 function_name.py 0.3 0.2 or python3 function_name.py 0.3 0.2 0.1 0.5 0.6 0.7 and so on

expected output

answer 1 
answer 2 
def function_name(inputa, inputb)
    #long formula which uses 2 inputs
    return answer

if __name__ == '__main__':
    function_name(float(sys.argv[1]), float(sys.argv[2]))

input:

python3 function_name.py 0.3 0.2

output:

answer 

input:

python3 function_name.py 0.3 0.2 0.4 0.5

output:

answer1
answer2  
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • Does this answer your question? [Can a variable number of arguments be passed to a function?](https://stackoverflow.com/questions/919680/can-a-variable-number-of-arguments-be-passed-to-a-function) – mkrieger1 Jul 12 '23 at 21:24

2 Answers2

3

This does what you asked, but I'm pretty sure what you asked is not really what you wanted. I've supplied a fake function just to show it working:

import sys
def function_name(inputa, inputb):
    #long formula which uses 2 inputs
    return inputa+inputb
if __name__ == '__main__':
    args = sys.argv[1:]
    while len(args) >= 2:
        a = args.pop(0)
        b = args.pop(0)
        print("Answer:", function_name(float(a), float(b)))

Output:

timr@Tims-NUC:~/src$ python x.py 0.5 0.7 1.2 1.5 2.5 2.9
Answer: 1.2
Answer: 2.7
Answer: 5.4
timr@Tims-NUC:~/src$
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • yea I believe I explained it wrong, I am using this equation for another program. this gives me 1 answer but I am looking for an answer for each inputa, inputb that is inputted n amount of times. sorry if this isn't making sense, It is part of a larger program. I had the formula working with just the 2 inputs but need to be able to run it with n amount of 2 inputs with n amount of answers per each 2 inputs – MkLinux Jul 12 '23 at 21:25
  • So, I'm guessing you didn't even try this code before responding. This does not give you one answer. If you supply 6 arguments, it prints out 3 answers, just as you asked. – Tim Roberts Jul 12 '23 at 21:30
  • I tried it but when I unit test it is shows 1 answer as well as running it in python I must be doing something wrong it shows me only the answer to the first two items – MkLinux Jul 12 '23 at 21:31
  • 2
    What do you mean by "unit test"? Your function accepts two inputs and returns one output. It's always going to do that. It's the main program that handles the multiple arguments. – Tim Roberts Jul 12 '23 at 21:34
  • thank you I see it now this was what I needed thank you! – MkLinux Jul 12 '23 at 21:34
  • 1
    This solution with pop(0) is unnecessarily slow (O(n**2)) for long argv arrays. You may want to do `for i in range(1, len(sys.argv), 2):` instead (which is O(n)). – pts Jul 12 '23 at 21:38
  • Now if you want to only call the function once, and it's the function that returns as many answers as there are pairs of arguments, that's possible too but that's another code. – Swifty Jul 12 '23 at 21:42
  • yeah that is what I am looking for @Swifty – MkLinux Jul 12 '23 at 21:45
  • That's not what you asked for. You gave a specific example of the output you wanted. – Tim Roberts Jul 12 '23 at 22:50
  • yea you are right I think I ran the other way and get a list with the wrong answers will stick with the way you showed @Tim new to this – MkLinux Jul 13 '23 at 15:34
  • I guess I explained it wrong there will be other python scripts that calling this function and it needs to be able to input any number of sets of 2 for an answer with each set of 2 – MkLinux Jul 13 '23 at 19:48
0

Here's an implementation where the function works with several (pairs of) arguments; the function actually returns a generator for the answers:

import sys

def function_name(*args):
    for i in range(0, len(args)//2):
        inputa, inputb = args[2*i], args[2*i+1]
        #long formula which uses 2 inputs
        yield inputa + inputb


if __name__ == '__main__':
    args = sys.argv[1:]
    print(list(function_name(*map(float, args))))

Example:

C:\Users\Swifty\.spyder-py3>python tmp.py 1 2 3 4 1.2 4.7
[3.0, 7.0, 5.9]

Note: I coded a protection against an odd number of arguments: the last one is ignored.

Example:

C:\Users\Swifty\.spyder-py3>python tmp.py 1 2 3 4 1.2
[3.0, 7.0]
Swifty
  • 2,630
  • 2
  • 3
  • 21