質問

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