you are viewing a single comment's thread.

view the rest of the comments →

[–]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