Search code examples
pythoncalculatordivisionpython-3.6subtraction

How can I get subtraction and division in my calculator using Python 3 to work using an if statement?


So I previously asked a question on why the subtraction and division in the calculator I made in python 3.6.1 were not working. A lot of you graciously responded, but I didn't get the answer I wanted. I apologize, for I should have been more specific, but is there a way for me to add some kind of if statements inside the while loops? This is my code:

print("Welcome to Calculator!")

class Calculator:
    def addition(self,x,y):
        added = x + y
        return added
    def subtraction(self,x,y):
        subtracted = x - y
        return subtracted
    def multiplication(self,x,y):
        multiplied = x * y
        return multiplied
    def division(self,x,y):
        divided = x / y
        return divided

calculator = Calculator()

print("1 \tAddition")
print("2 \tSubtraction")
print("3 \tMultiplication")
print("4 \tDivision")
operations = int(input("What operation would you like to use?:  "))

x = int(input("How many numbers would you like to use?:  "))

if operations == 1:
    a = 0
    sum = 0
    while a < x:
        number = int(input("Please enter number here:  "))
        a += 1
        sum = calculator.addition(number,sum)
    print("The answer is", sum)
if operations == 2:
    s = 0
    diff = 0
    while s < x:
        number = int(input("Please enter number here:  "))
        s += 1 
        diff = calculator.subtraction(number, diff)
    print("The answer is", diff)
if operations == 3:
    m = 0
    prod = 1
    while m < x:
        number = int(input("Please enter number here:  "))
        m += 1
        prod = calculator.multiplication(number, prod)
    print("The answer is", prod)
if operations == 4:
    d = 0
    quo = 1
    while d < x:
        number = int(input("Please enter number here:  "))
        d += 1
        quo = calculator.division(number,quo)
    print("The answer is", quo)

Basically, subtraction and divide kinda work in the opposite way, and if I tried to input 2 numbers, 9 and 3 for subtraction I would get -6, and for division I would get 0.33333333(1/3). Sorry if this is a dumb question though, because I'm a complete beginner when it comes to coding.


Solution

  • For addition & multiplication order does not matter i.e. 9+6=6+9 and 3*2=2*3. But not the case with subtraction & division i.e. 9-6 not equals 6-9.

    In your case for subtraction for the number entered 9 & 6:

    For 1st input: 9, number = 9 & diff = 0 so diff = number - diff = 9 - 0 = 9

    For 2nd input: 6, number = 6 & diff = 9 so diff = number - diff = 6 - 9 = -3

    Which is not our intention

    A slight modification in your code

    # For Subtraction
    if operations == 2:
        s = 0
        diff = 0
        while s < x:
            number = int(input("Please enter number here:  "))
            s += 1
            if (s==1):
                diff=number
            else:
                diff = calculator.subtraction(diff, number)
        print("The answer is", diff)
    
    #For Division
    if operations == 4:
        d = 0
        quo = 1
        while d < x:
            number = int(input("Please enter number here:  "))
            d += 1
            if (d==1):
                quo=number
            else:
                quo = calculator.division(quo,number)
        print("The answer is", quo)