问答详情
源自:4-4 Python之for循环

L = [75, 92, 59, 68, 99] for a in L: sum = 0 sum = sum + a print (sum / 5)

L = [75, 92, 59, 68, 99]

for a in L:

   sum = 0

   sum = sum + a

print (sum / 5)

为什么这样的答案算出来就是19呢?

L = [75, 92, 59, 68, 99]

sum = 0

for a in L:

   sum = sum + a

print (sum / 5)

这样输就正常的,是放在for里面就不循环吗


提问者:慕前端6048448 2020-12-02 17:15

个回答

  • qq_慕雪4134183
    2020-12-02 19:30:56
    已采纳

    sum=0放在for循环里不停的取值,又不停的在for开始的时候被赋为0,最终sum的值取值为99,

    99/5=19.8

    sum=0默认int,所以19.8取值19

  • weixin_慕田峪7094823
    2021-05-23 22:31:09

     L=[75, 92, 59, 68, 99]
    >>> sum=0.0
    >>> for a in L:
    ...     sum=sum+a
    ...     print(sum/5)

    我这个输出的结果怎么是

    15.0
    33.4
    45.2
    58.8
    78.6

    5个数