-1

Code:

sum=0
for i in range(10,91):
    sum=sum+i
    print(sum)

When I wrote this code, the answer was Output:

10
21
33
46
60
75
91
108
126
145
165
186
208
231
255
280
306
333
361
390
420
451
483
516
550
585
621
658
696
735
775
816
858
901
945
990
1036
1083
1131
1180
1230
1281
1333
1386
1440
1495
1551
1608
1666
1725
1785
1846
1908
1971
2035
2100
2166
2233
2301
2370
2440
2511
2583
2656
2730
2805
2881
2958
3036
3115
3195
3276
3358
3441
3525
3610
3696
3783
3871
3960
4050

What is the problem? Please help me

deadshot
  • 8,881
  • 4
  • 20
  • 39
dy k
  • 27

2 Answers2

1

This is what your code should look like if you do not want to see each calculation.

sum = 0
for i in range(10,91):
    sum = sum + i
print(sum)

On a sidenote, as suggested by @Albin Paul, sum is a builtin function, therefore it is recommended to avoid using it as a variable name, since it overwrites the function definition.

Alexander
  • 16,091
  • 5
  • 13
  • 29
  • 1
    sum is a function in python, if you use sum as a variable you cant use the sum function again. – Albin Paul Mar 14 '22 at 05:37
  • 1
    You are a hero - with almost no information in this question, you managed to recognize that the asker probably didn't want any output besides the last one, and decided it was worth answering that possibility. That's either a really lucky guess or great idea-fitting and decision making. Cheers. – mtraceur Jul 04 '22 at 06:50
1

Loops in Python work differently than you think.

Let's start with a simpler loop example. When you write:

for i in range(1, 4):
    print(i)

it is as if you actually wrote:

i = 1
print(i)
i = 2
print(i)
i = 3
print(i)

The loop body is print(i), so Python takes print(i) and runs it once for each i in the range.

So, since range(1, 4) contains the numbers 1, 2, and 3, first i got assigned the value 1, then the loop body was executed, then i got assigned the value 2, then the loop body was executed again, and so on.

Think of a loop as just shorthand for copy-pasting the same code a bunch of times.

So in your specific problem, your code:

sum=0
for i in range(10,91):
    sum=sum+i
    print(sum)

is as if you copy-pasted the same code like this:

sum=0
i=10
sum=sum+i
print(sum)
i=11
sum=sum+i
print(sum)
i=12
sum=sum+i
print(sum)
i=13
sum=sum+i
print(sum)
...
i=88
sum=sum+i
print(sum)
i=89
sum=sum+i
print(sum)
i=90
sum=sum+i
print(sum)

Notice the print(sum) copy-pasted over and over again.

And that is why your code has the output that it does.

Hopefully that helps you understand why the other answer suggested changing it to this instead:

sum = 0
for i in range(10,91):
    sum = sum + i
print(sum)

Because when you take the print out of the loop body, it's as if you're only copy-pasting the sum=sum+i:

sum=0
i=10
sum=sum+i
i=11
sum=sum+i
i=12
sum=sum+i
i=13
sum=sum+i
...
i=88
sum=sum+i
i=89
sum=sum+i
i=90
sum=sum+i
print(sum)

Notice that there's only one print at the end now.

Also!

You can use this same thinking to understand what went wrong in your other question which was "closed as duplicate"!

There you wrote:

for i in data:
  print(max(i))

which is as-if you wrote:

i=data[0]
print(max(i))
i=data[1]
print(max(i))
i=data[2]
print(max(i))
...

and so in that problem, you can see that max only ever gets called with one integer from data at a time.

mtraceur
  • 3,254
  • 24
  • 33