0

I am trying to develop a simple calculator function in python where the function signature can take more than 2 nos, default operation will be ADD and default output format would be float.

def calculator(operation=ADD,output=float,*args):

Basically the function should apply the operation to the first two numbers, and then apply it again to the result and the next number, and so on. For example, if the numbers are 6, 4, 9 and 1 and the operation is subtraction the function should return 6 - 4 - 9 - 1.

However when I call the function with:

calculator(1,2,3,4,operation=ADD,output=float)

I get an error:

TypeError: calculator() got multiple values for argument 'operation'

Can someone please tell me how should i invoke this function with right set of parameters?

0

4 Answers 4

2

Put *args first. It's interpreting your integers as values for operation, which is expecting a string.

Sign up to request clarification or add additional context in comments.

Comments

1

You can define a function:

def calculator(operation, output, *args):
    pass

And you can use it with:

calculator('ADD', 'float', 1, 2, 3, 4)

Your arguments are positional, so if you call:

calculator(1,2,3,4,operation=ADD,output=float)

You are actually saying that operation=1 and output=2, that´s why the interpreter complains about passing the value twice.

Comments

0

when you call your function

def calculator(operation=ADD, output=float, *args):
    ...

like calculator(1,2,3,4,operation=ADD,output=float) the argument are assigned first by position 1,2,3,4 and then by key-word operation=ADD, output=float so you get that operation=1, ouput=2 and args get the rest (3,4), then it try to assign the key-word arguments but it turn out that operation was already assigned hence the error.

So with your current definition call it as calculator(ADD,float,1,2,3,4) or change the definition to

def calculator(*args, operation=ADD, output=float):
    ...

this way operation and output are key-word only arguments and you can call it as before calculator(1,2,3,4,operation=ADD,output=float).

But this syntax is python 3 only, to do the same in python 2 you need to do

def calculator(*args, **kwarg):
    operation = kwarg.get("operation",ADD)
    output = kwarg.get("output",float)
    ...

Comments

0

You probably should use args, kwargs for this. A good tutorial for this is given here. https://www.unixmen.com/introduction-python-args-kwargs-beginners-part-1/

>>> def calculator(*args, **kwargs):
...     for a in args:
...         print(a)
...     for k, v in kwargs.items():
...         print("%s := %s" % (k, v))
...
>>> calculator(1,2,3, op=add, ouput=float)
1
2
3
ouput := <class 'float'>
op := <built-in function add>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.