Why wont codeacademy accept my code? https://www.codecademy.com/paths/computer-science/tracks/cspath-flow-data-iteration/modules/dspath-lists/lessons/python-functions-lists-cc/exercises/double-index

https://www.codecademy.com/paths/computer-science/tracks/cspath-flow-data-iteration/modules/dspath-lists/lessons/python-functions-lists-cc/exercises/double-index

Have been given this challenge, called Double Index under Python Lists

Challenge is:
Create a function named double_index that has two parameters named lst and index .

The function should double the value of the element at index of lst and return the new list with the doubled value.

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

My code is:

def double_index(lst, index):
  try:
    lst[index] = 2 * lst[index]
  except IndexError:
    lst[index] = lst[index]
  return lst

I have tried it out with multiple values and it seems to work - why wont it be accepted?

lets add a function call so we can see something:

def double_index(lst, index):
  try:
    lst[index] = 2 * lst[index]
  except IndexError:
    lst[index] = lst[index]
  return lst

print(double_index([1, 2, 3], 5))

you catch the IndexError, but then you access by index again, which you know causes an error

1 Like