-2

How can i Divide each element by the next one in division function? i am passing arbitrary arguments in the calling function. Thanks in advance.

    def add(*number):
        numm = 0
        for num in number:
            numm =num+numm
        return numm

    def subtract(*number):
        numm = 0
        for num in number:
            numm = num-numm
        return numm

    def division(*number):
        #i am facing problem here
        # numm = 1
        # for num in number:

        try:
        if (z>=1 and z<=4):
            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()

            if z==1:
                print("Addition of  numbers is:", add(*val_list))
            elif z==2:
                print("Subtraction of numbers is:", subtract(*val_list))
            elif z==3:
                print("division of numbers is:", division(*val_list))
yajant b
  • 396
  • 1
  • 4
  • 12
  • Your problem is not clear. What do you mean by "Divide each element by the next one"? It would have been more clear if you gave example input and output. Please read and follow [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). Do you want `division(96, 2, 3, 2)` to calculate `96 / 2 / 3 / 2`, i.e. `((96 / 2) / 3) / 2`, and return `8.0`? Or do you want `1 / 96 / 2 / 3 / 2` and return `0.0008680555555555555`? – Rory Daulton Jun 14 '18 at 13:38
  • Div (/) and Mod (%) should be your friends. divmod() too. – suren Jun 14 '18 at 13:46
  • @RoryDaulton Yes, i wanted to do this only. i.e (96 / 2 / 3 / 2, i.e. ((96 / 2) / 3) / 2, and return 8.0). Got the answer, thanks. i will look into the link that you provided and will try to be more specific with what i am asking. – yajant b Jun 14 '18 at 13:51

2 Answers2

0

I'm not sure I quite understand what you want, but if you are expecting to call division() with the parameters 100, 3, 2 and compute (100 / 3) / 2 (answer: 16.6666) then

def division(*number):
    numm = number[0]
    for num in number[1:]:
        numm /= num
    return numm

It's not analogous to the other functions because they start with numm set to zero. Setting it to 1 will work for multiplication but for division it won't help. You need to set it to the first parameter and then divide successively by the remaining parameters.

BoarGules
  • 16,440
  • 2
  • 27
  • 44
0

In Python 3 you can gracefully reach this goal with using reduce from functools library. From documentation:

Apply function of two arguments cumulatively to the items of sequence, from left to right, so as to reduce the sequence to a single value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5). The left argument, x, is the accumulated value and the right argument, y, is the update value from the sequence. If the optional initializer is present, it is placed before the items of the sequence in the calculation, and serves as a default when the sequence is empty. If initializer is not given and sequence contains only one item, the first item is returned.

And as the example how it can be used in your code, so that it will look much better:

from functools import reduce


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

def add(iterable):
  return sum(iterable, start=0)
  # or reduce(lambda x, y: x + y, iterable, 0)

def subtract(iterable):
  return reduce(lambda x, y: x - y, iterable, 0)

def division(iterable):
  return reduce(lambda x, y: x / y, iterable, 1)


if __name__ == '__main__':
  try:
    print("Operation_type: ")
    value = input()
    operation_type = int(value)
  except ValueError:
    operation_type = -1

  if (operation_type >= 1 and operation_type <= 4):
    values = get_input()

    if operation_type == 1:
      print("Addition of  numbers is: {}".format(add(values)))
    elif operation_type == 2:
      print("Subtraction of numbers is: {}".format(subtract(values)))
    elif operation_type == 3:
      print("division of numbers is: {}".format(division(values)))

  else:
    print("An invalid argument was specified: `operation_type` must be in range between 1 and 4")
Relrin
  • 760
  • 2
  • 10
  • 28