为什么代码显示所有添加过程?

青葱年少 python 506

原文标题Why the code shows all the addition process?

代码:

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

当我写这段代码时,答案是输出:

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

问题是什么?请帮我

原文链接:https://stackoverflow.com//questions/71463346/why-the-code-shows-all-the-addition-process

回复

我来回复
  • alexpdev的头像
    alexpdev 评论

    如果您不想看到每个计算,这就是您的代码应该看起来的样子。

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

    在旁注中,正如@Albin Paul 所建议的那样,sum是一个内置函数,因此建议避免将其用作变量名,因为它会覆盖函数定义。

    2年前 0条评论