문제

I'm working through John Guttag's Intro to Computation and Programming Using Python. On page 64 he talks about Functions as objects.

Why is the application of the abs function skipping "2" and printing "3.3300000001" three times?

I'm using Idle Python Shell 3.3.3.

def applyToEach(L, f):

    for i in range(len(L)):

        L[1] = f(L[i])

L = [1, 2, 3.3300000001]        



print('L =', L)  ###L = [1, 2, 3.3300000001]

applyToEach(L, abs)

print('L =', L)   ###L = [1, 3.3300000001, 3.3300000001]
도움이 되었습니까?

해결책

Because you wrote L[1] instead of L[i].

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top