0

I am writing a program of calculator for adding, subtracting, multiply and dividing infinite number of values.

I have tried multiple ways like :

values = [int(values) for values in input("Enter values: ").split()]

But this return a list of values and we can perform direct addition of values of list using built-in function sum() but I think we can't perform direct subtraction, multiplication and divide on list. So, for subtraction, multiplication and divide I create following function:

def multi(firstValue, *otherValues):
    result = firstValue

    for i in otherValues:
        result = result * i

    print(result)

In this function it takes two arguments firstValue as integer and otherValue as tuple. When I give fixed arguments like:

multi(1, 2, 3, 4, 5)

it gives correct answer but when I take inputs from user than I am not to able to give correct argument like I mention up:

values = [int(values) for values in input("Enter values: ").split()]
multi(values)

Please give me some solution or some suggestion for taking inputs in other way or changing the way of performing tasks in other way.

Veer Singh
  • 11
  • 3
  • use `multi(*values)` – bn_ln Mar 31 '23 at 12:14
  • It give output like this : `Enter values: 1 1 1 1 1 Traceback (most recent call last): File "D:\Projects\5th sem\Python\test.py", line 12, in multi(*values) TypeError: multi() takes 1 positional argument but 5 were given Process finished with exit code 1 ` – Veer Singh Mar 31 '23 at 12:28
  • Then you've changed your definition of `multi`. The `multi` you show in your question *does* accept any number of positional arguments. – deceze Mar 31 '23 at 12:29
  • OK. This worked. But can you explain me that how it works. – Veer Singh Mar 31 '23 at 14:36
  • Your function is written to take several parameters, so you need to unpack the `values` list. – bn_ln Apr 01 '23 at 00:12

0 Answers0