Domanda

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]
È stato utile?

Soluzione

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top