Reputation: 11
I input 10 to the below sequence, the it prints sum = 56 I list what sum includes, they are 1,2,3,4,5,6,7,8,9,10. I wonder what's wrong with the calculation of sum.
=======================================
n = int(input('input number?'))
sum = 1
for i in range(1,n+1,1):
sum += i
print(sum)
Upvotes: 1
Views: 871
Reputation: 19
I don't know why the variable 'sum' is initialized to 1. It should be 0. You will get the correct result. :)
Upvotes: 0
Reputation: 484
That's because you first define sum = 1. The true result is 55 but you increment it by yourself.
true code is below
n = int(input('input number?'))
result = sum(range(1, n+1))
You should not use sum as a variable name because sum is a built-in function. And in this case, you can calculate summantion by sum so easily.
Upvotes: 1