Ömer Alkin
Ömer Alkin

Reputation: 303

Subtracting more than 2 numbers in Python

I am little bit new to the programming. I am learning Python, version 3.6.

print("1.+ \n2.-\n3.*\n4./")
choice = int(input())
if choice == 1:
    sum = 0
    print("How many numbers you want to sum?")
    numb = int(input())
    for i in range(numb):
        a = int(input(str(i+1)+". number "))
        sum+=a
    print("Result : "+str(sum))

For improving myself i am trying to build a calculator but first i am asking how many numbers user want to calculate. You can see this in code above, but when it is comes to subtracting or dividing or multiplying i have no idea what to do.

My reason to do it like this i want to do the calculator like in real time calculators.

Upvotes: 0

Views: 3259

Answers (3)

Naveen Kaulwar
Naveen Kaulwar

Reputation: 1

There are two methods to solve this:

Method-1

Using the logic for subtraction a-b-c-… = ((a-b)-c)-…

def subt1(*numbers):     # defining a function subt1 and using a non-keyword argument *numbers so that variable number of arguments can be provided by user. All these arguments will be stored as a tuple.

    try:                     # using try-except to handle the errors. If numbers are given as arguments, then the statements in the try block will get executed.

        diff = numbers[0]    # assigning the first element/number to the variable diff

        for i in range(1,len(numbers)):     # iterating through all the given elements/ numbers of a tuple using a for loop
            diff = diff - numbers[i]        # performing the subtraction operation for multiple numbers from left to right, for eg, a-b-c = (a-b)-c
        return diff                         # returning the final value of the above operation

    except:                                 # if no arguments OR more than one non-numbers are passed, then the statement in the except block will get executed
        return 'please enter numbers as arguments'

subt1(10, 5, -7, 9, -1) ----> here subt1 performs 10-5-(-7)-9-(-1) and returns the value

4

subt1(25.5, 50.0, -100.25, 75) ----> here subt1 performs 25.5-50.0-(-100.25)-75 and returns the value

0.75

subt1(20j, 10, -50+100j, 150j) ----> here subt1 performs 20j-10-(-50+100j)-150j and returns the value

(40-230j)

subt1() ----> here the statement in the except block is returned as no input is passed

'please enter numbers as arguments'

subt1('e', 1, 2.0, 3j) ---> here the statement in the except block is returned as a string 'e' is passed which is not a number

'please enter numbers as arguments'

Method-2

Using the logic for subtraction a-b-c-… = a-(b+c+…) = a-add(b,c,…)

def subt2(*numbers):

    try:
        add = 0       # initializing a variable add with 0

        for i in range(1,len(numbers)):
            add = add+ numbers[i]       # performing the addition operation for the numbers starting from the index 1
        return numbers[0]-add            # returning the final value of subtraction of given numbers, logic : a-b-c = a-(b+c) = a-add(b,c)

    except:
        return 'please enter numbers as arguments'

subt2(10, 5, -7, 9, -1) ----> here subt2 performs 10-5-(-7)-9-(-1) and returns the value

4

subt2(25.5, 50.0, -100.25, 75) ----> here subt2 performs 25.5-50.0-(-100.25)-75 and returns the value

0.75

subt2(20j, 10, -50+100j, 150j) ----> here subt2 performs 20j-10-(-50+100j)-150j and returns the value

(40-230j)

Note : All the above test cases have been tested in Jupyter notebooks.

Upvotes: 0

Shamik Datta
Shamik Datta

Reputation: 43

You can also use the *args or *kargs in order to subtract more than two numbers. If you define *args keyword in a function then it will help you to take as many variables you want.

Upvotes: 1

Personman
Personman

Reputation: 2323

You can do the exact same thing you're already doing. Python has -=, *=, and /= operators that work the same way as the += you're already using.

Upvotes: 2

Related Questions