Home > Mobile >  Python: mathematical operations
Python: mathematical operations

Time:10-06

I want to creat a programa that reads two input numbers and do mathematical operations on them: addition, multiplication, division and subtraction.

I have this code:

sum=0
multi=0
div=0
sub=0

for i in range(1,3):
    num = int(input(f'{i}º dígito: '))
    sum =num
    multi*=num
    div/=num
    sub-=num

print(f'Soma: {sum}')
print(f'Multi: {multi}')
print(f'Div: {div}')
print(f'Sub: {sub}')

The sum works well, but the others don't and I don't understand why. If I input 3 as the first number and 2 as the second number the sum is 5 (all good), but the multi prints 0, the div 0.0 and the sub -5.

What am I doing wrong?

CodePudding user response:

multi: you start with 0, therefore 0*anything == 0.

div: you also start with 0, and 0/anything == 0.

sub: looks right to me. 0-3-2 == -5

I suspect what you want is to have the first number operated on using the second number. Your current set-up is using 0 operated on using the first number and then the second number.

first = int(input(f'{i}º dígito: '))
second = int(input(f'{i}º dígito: '))
summ = first   second #don't use sum as a variable name - its reserved in python
multi = first * second
sub = first - second
div = first / second

This I suspect is more like what you want. Be careful of first==0 because your division will fail. Also, I'd look up "integer division" and its impact in Python because it might cause you some confusion.

CodePudding user response:

#Just add some lines

sum=0
multi=1
div=1
sub=0

for i in range(1,3):
    num = int(input(f'{i}º dígito: '))
    sum =num
    multi*=num
    if (i==1):
        div = num
    if( i==2):
        div = div/num
    sub = -sub
    sub-=num

print(f'Soma: {sum}')
print(f'Multi: {multi}')
print(f'Div: {div}')
print(f'Sub: {sub}')

CodePudding user response:

That's happening because when you do a operation like this multi*=num the python interpreter is doing this: multi = multi * num. If the variable multi starts with zero then: multi = 0 * num, and returns 0.

So, what's happening with each operatoration on the first iteration:

  • Sum = 0 num = num.
  • Div = num / 0 = 0
  • Multi = 0 * num = 0
  • Sub = 0 - num = -num

You need to fix the first iteration of your code, and something like this will work:

    sum=0
    multi=0
    div=0
    sub=0
    # creates a counter
    count = 0
    
    for i in range(1,3):
        num = int(input(f'{i}º dígito: '))
        # if it is not the first iteration them do the operation
        if count > 0:
            sum =num
            multi*=num
            div/=num
            sub-=num
        # else sets everything equal num
        else:
            sum = num
            multi = num
            div = num
            sub = num
        # updates the counter
        count  = 1
    
    print(f'Soma: {sum}')
    print(f'Multi: {multi}')
    print(f'Div: {div}')
    print(f'Sub: {sub}')

CodePudding user response:

As mentioned in the comments, defining the variables as 0 from teh beginning and operating straightforward on them may cause problems because it will do things like 0 * num or 0 / num.

For only two inputs you don't need a loop. But if you want to loop anyway (let's say that you're considering adding the possibility of working with more inputs in the future), I would consider that the inputted values that you want to store are 1) conceptually similar: they're all numbers inputted by the user that you'll use in the calculations; 2) they have an order. So, the data type you need is probably a list.

num = []
i = 1
while i <= 2:
    num.append(int(input(f'{i}º dígito: ')))
    i  = 1
    
suma = num[0]   num[1]
multi = num[0] * num[1]
sub = num[0] - num[1]

try:
    div = num[0] / num[1]
except ZeroDivisionError:
    div = "No puedes dividir por cero!"

print(f'Suma: {suma}')
print(f'Multi: {multi}')
print(f'Div: {div}')
print(f'Sub: {sub}')
  • suma instead of sum since sum is a reserved word. One that you may want to use if dealing with many inputs :)

  • while i <= 2 instead of for for i in range(1,3) for readability. This will come handy if you need to adapt the code (e.g. you could ask the user to input the value for i).

  • try - except block to handle the case of division by zero.

  • Related