This program should add up all the numbers from a to b, but it gives wrong answers.
What to build
A corrected copy of the starter code.
Requirements
- Find and fix the bugs. There are two.
- Print the sum of the whole numbers from
atob, including both.
Input
One line with a and b.
Output
One number.
Constraints
a is at most b. Both are from -1000 to 1000.
Example
Input:
1 3
Output:
6
Why it works
range stops before its second value, so the end needs a + 1. Starting the total at the wrong value is the other classic slip.
Examples
Example 1 · Input
1 3
Expected output
6
Example 2 · Input
5 5
Expected output
5
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Try the input 1 3 and work out the right answer by hand.
Hint 2
Watch the loop variable in the visualizer. What is its last value?
Solution
Show one correct solution (try it yourself first)
a, b = [int(x) for x in input().split()]
total = 0
for i in range(a, b + 1):
total += i
print(total)