pythonfunctioncalculatormultiple-arguments

Divide each element by the next one in python


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))

Solution

  • 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.