Because you wrote L[1]
instead of L[i]
.
abs Function as an Object
-
27-06-2023 - |
Question
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]
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow