How to differentiate 2 inputs created with for loop and perform operations between them

Asked

Viewed 31 times

0

How do I sum the 2 values typed by the user? My code:

for c in range(0, 2):
    n = int(input('Digite um valor:'))

How to differentiate the first value of the second to do operations as sum and subtraction between them?

1 answer

2


There are many ways to do this.

One of them is to create an array with all the two values entered by the user:

n = []
for c in range(0, 2):
    n.append(int(input('Digite um valor:')))

print("A soma é {}".format(sum(n)))

You can create a variable that performs the operation on each loop:

sum = 0

for c in range(0, 2):
    n = int(input('Digite um valor:'))
    sum += n

print("A soma é {}".format(sum))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.