Why is my function getting an error line? https://www.codecademy.com/paths/computer-science/tracks/cspath-flow-data-iteration/modules/dspath-lists/lessons/python-functions-lists-cc/exercises/double-index


#1

My function returns (double the value of the element at index of lst and returns the new list with the doubled value) only if index is not too big.

When I click run it returns the doubled value in the new list but this comment is added to the bottom of the screen
“Make sure to define what should happen if index is too big!”

I click SOLUTION and get this code
def double_index(lst, index):
if index < len(lst):
lst[index] = lst[index] * 2
return lst

My code is excessive and overthought
but why do I keep getting this error
“Make sure to define what should happen if index is too big”
def double_index(lst, index):
length = len(lst)
if index == 100:
return “index is too big!”
elif index > 100:
return “index is too big!”
elif length < index:
return “index is too big!”
else:
value = lst[index] * 2
beginning = lst[0:index]
end = lst[index+1:]
lst =
lst = (beginning)
lst.append(value)
lst = (lst + end)
return lst


#2

please read carefully what is expected if the index is too big/high:

If index is not a valid index, the function should return the original list.

lets say i have a list with 5 items:

[2, 4, 6, 8, 10]

then the length is 5, however, the highest index is 4. lists are zero indexed based which means counting start at zero.

so length < index won’t do, given 5 is an invalid index so the if condition should evaluate to true, that is currently not happening


#3

Thank you for your explanation.