I’m writing a function that squares each element in the list.
def square(num):
for i in range(len(num)):
square[i] = square[i] ** 2
def action():
nums = [2, 3, 4]
print square(nums)
action()
It returns an error:
square[i] = square[i] ** 2
TypeError: 'function' object has no attribute `__getitem__`.
Ideas?
Within your
squarefunction you are referring to the function as if it’s a list:Because
squareis not a list, Python tries to ask it for itemiwith the__getitem__method, but that doesn’t exist either.Perhaps you meant to use
numinstead?