1

Trying to create a calculator, which can take variable length of integers separated by space. I am able to create a basic calculator that would read two args and do operations. Below is what I am trying to achieve.

select operation: Add
Enter nos: 1 65 12 (this length can increase and any variable lenght of integers can be given)

I am not sure how would I pass this variable length of int to functions, suppose addition function. I can do it for two variables.

Adding what I am aware of:

x = input("enter operation to perform")
a = input("1st no.")
b = input("2nd no.")
def add(a,b):
    return a+b
if x == 1:
    print add(a,b)

Not sure how can I pass multiple args read from input to function.

5
  • Use sys module sys.argv[1] Commented Jul 2, 2017 at 12:37
  • @ArpitSolanki That's not what he wants. He simply wants user input. That can be achieved using input(). Commented Jul 2, 2017 at 12:38
  • @ArpitSolanki: how can i pass that to function? Commented Jul 2, 2017 at 12:40
  • @ChristianDean: yes using input(), which will have variable length of args, and then those to be passed to functions and so that a math operation be performed on those int. Commented Jul 2, 2017 at 12:41
  • I've edited my original answer to show how the input can be done. Commented Jul 2, 2017 at 12:51

2 Answers 2

2

Using input you can achieve this:

>>> result = input("enter your numbers ")
enter your numbers 4 5
>>> result
'4 5'
>>> a, b = result.split()
>>> a
'4'
>>> b
'5'
>>> int(a) + int(b)
9

The split method will split your string by default on space and create a list of those items.

Now, if you had something more complicated like:

>>> result = input("enter your numbers ")
enter your numbers 4 5 6 7 8 3 4 5
>>> result
'4 5 6 7 8 3 4 5'
>>> numbers = result.split()
>>> numbers
['4', '5', '6', '7', '8', '3', '4', '5']
>>> numbers = list(map(int, numbers))
>>> numbers
[4, 5, 6, 7, 8, 3, 4, 5]
>>> def add(numbers):
...  return sum(numbers)
...
>>> add(numbers)
42

As you can see you are taking a longer sequence of numbers split by space. When you call split on it, you will see you have a list of numbers but represented as strings. You need to have integers. So, this is where the call to map comes in to type the strings to integers. Since map returns a map object, we need a list (hence call to list around the map). Now we have a list of integers, and our newly created add function takes a list of numbers, and we simply call sum on it.

If we wanted something that required a little more work, like subtraction, as suggested. Let us assume we already have our list of numbers, to make the example smaller to look at:

Furthermore, to help make it more readable I will do it step by step:

>>> def sub(numbers):
...  res = 0
...  for n in numbers:
...   res -= n
...  return res
...
>>> sub([1, 2, 3, 4, 5, 6, 7])
-28
Sign up to request clarification or add additional context in comments.

7 Comments

how can this variables(which would be of any length) read and split be passed to functions !!!
@shivrk You don't need those extra exclamation marks. Being frustrated is not a correct approach here. You are getting people to answer your questions. Getting angry is not appreciated.
@shivrk you are already passing a and b to your function add: if x == 1: print add(a,b). I do not understand what you do not understand.
@AGNGazer The question states that they are trying to figure out how to do it for a variable amount of inputs. So, if they enter "1 2 3 4 5", taking just a and b won't work. So, the answer here is to either pass an entire list, or unpack it through an arbitrary list of arguments with * as indicated in another answer.
@idjaw: Thank u for replying,Trying out your answer. didn't realize that xtra xclamation could mean that, will be careful replying onwards now.
|
0

If you use *args it can take an arbitrary amount of positional arguments. You can make similar procedures for other operations.

def addition(*args):
    return sum(args)

calc = {
        '+':addition,
        #'-':subtraction,
        #'/':division,
        #'*':multiplication,
        }

def get_input():
    print('Please enter numbers with a space seperation...')
    values = input()
    listOfValues = [int(x) for x in values.split()]
    return listOfValues


val_list = get_input()

print(calc['+'](*val_list))

This is the way I would implement the calculator. Have a dictionary that holds the operations (I would use lambdas), then you can pass the list of numbers to the specific operation in the dictionary.

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.