Python sum operation

Asked

Viewed 17,442 times

-1

Can someone explain to me how you make the sum of two integers between them, that is from the first number to the second number (which is not included)?

Here is an example:

numero1 = int (input("Digite o primeiro numero: "))
numero2 = int (input("Digite o segundo numero: "))

Entrada:
N1 = 1
N2 = 3

Saida = 3
  • Your question is not clear. Could [Dit] and give examples of what you need?

  • print(a + b).

  • @Andersoncarloswoss I edited.

  • 2

    And why is the output 3 for input 1 and 3? Would the sum 1+2? And if the input is 1 and 7, what should be the output?

  • The input and output values and description were taken from the run.codes where my teacher passes the questions, and sometimes he puts some wrong information in the questions

  • @Sonogoku They are not necessarily wrong, but they are insufficient to understand the issue. Edit the question and transfer the full statement of the exercise.

  • Wait a minute! What is this becoming? A simple "do and deliver me" ?

  • resultado = list(range(numero1,numero2))
print (sum(resultado)) that was the calculation that did what I wanted

Show 3 more comments

4 answers

2

sum(range(inicio, final + 1))

only.

  • resultado = list(range(numero1,numero2))
print (sum(resultado)) that was the calculation that did what I wanted

2

Dude, your question is not clear at all, but correct me if I got it wrong.

You want to make a sum. Add from the value n1 to the value N2.

Example: n1 = 1 and n2 = 5. The result will be 10, since 1+2+3+4 = 10.

That’s what you implied.

Logic

You declare a counter variable that starts with n1 and makes it incremented until it reaches the limit, which is n2.

Solution in Python

Note: You do with the loop you prefer, I chose the while because it was faster.

n1 = int(input("Digite o valor de n1: "))
n2 = int(input("Digite o valor de n2: "))

soma = 0

i = n1

while i < n2: //Para somar o n2 também, é só colocar <=
    soma = soma + i
    i = i + 1

print("O valor da soma é = ", soma)

That’s it. Let me know if that’s it.

  • If that’s what it is, the jsbueno’s response is already doing much better.

  • ,But this is a little more didactic

  • I tested here, really does the same thing kkkk it’s good that he understands what’s going on behind that single line.

  • The information was taken from the run.codes, I honestly think that some values that the teacher creates to ask the questions are wrong

  • @Sonogoku do as was said in the other comment, post the exact question ta no run.codes. Probably the jsbueno response or mine will solve the problem.

  • resultado = list(range(numero1,numero2))&#xA;print (sum(resultado)) that was the calculation that did what I wanted

  • Well, that answer you already had https://answall.com/a/374912/129017

Show 2 more comments

0


numero1 = int (input("Digite o primeiro numero: "))

numero2 = int (input("Digite o segundo numero: "))

resultado = 0

resultado = list(range(numero1,numero2))

print (sum(resultado))

0

Maybe that would be?

numero1 = int (input("Digite o primeiro numero: ")) # 1
numero2 = int (input("Digite o segundo numero: "))  # 3

nrSaida = len ( range(numero1 ,numero2) ) + 1

print(nrSaida) # 3
  • 2

    len ( range(numero1 ,numero2) ) + 1 is just a more complicated way of writing numero2-numero1+1.

  • No run.codes only 5/10 cases are executed correctly while the other 5/10 give incorrect results

  • The truth is that the guy doesn’t even know what he wants, and we’re here arguing to guess just to get a few points in the community. #Pronto ofalei. = P

  • resultado = list(range(numero1,numero2))&#xA;print (sum(resultado)) that was the calculation that did what I wanted

Browser other questions tagged

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