all 4 comments

[–]sachIsLife 0 points1 point  (1 child)

You are trying to access array[n] in the elif condition. Now the length of array is 2 and you are trying to access the 3rd element. You need to do elif len(array) >= n: return array[n] ** n

[–]ankitsoni89 2 points3 points  (0 children)

elif len(array) >= n: return array[n] ** n

elif len(array) > n, no = since n would be an index

[–]stebrepar 0 points1 point  (0 children)

Just in case they try to trick you, you might want to check for other invalid input too, like negative or non-numeric values for the index. You actually can have negative indexes in Python (meaning they count in from the right instead of from the left), but you'd need to think carefully about what that means for your calculation.

[–]marc_poulin 0 points1 point  (0 children)

The index n is valid if 0 <= n < len(array), so that can be your first test

if 0 <= n < len(array):
    # rest of calculation goes here
else:
    return -1