0

Here's the problem:

Write a program that asks the user for two numbers. Using a for loop, add all of the numbers from the first to the second. For example, if the first number is 6 and the second number is 8 the result is 21 (6 + 7 + 8). Print out the results when you are finished.

My code doesn't work though and I've been trying for a while now.

sum = 0

first_num = int(input("First number: "))

second_num = int(input("Second number: "))

for i in range(first_num, second_num):
    
    sum += i
 
print("Sum is: " + str(sum))

In my code, If I put 6 and 8 it will give me 13, not 21.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
  • 1
    `range()` does not include the last number...it's up to, but not including `second_num`. You might try adding one to it with something like `range(first_num, second_num + 1)` – Mark Oct 27 '20 at 23:50
  • 1
    Thank you so much, that worked! – Sophia Simpson Oct 27 '20 at 23:54
  • 2
    You might also want to take a look at the [Python docs for `range()`](https://docs.python.org/3/library/stdtypes.html#typesseq-range) – Gino Mempin Oct 27 '20 at 23:57

0 Answers0