What is wrong with my proposed solution to this coding challenge?

I amd doing coding challenge 4 at this link: https://www.codecademy.com/courses/learn-python-3/articles/advanced-python-code-challenges-lists

I don’t know what’s wrong with my proposed solution, but it’s not being marked as correct? Is it because I’m modifying the original list rather than returning a new one? Here is my code from my solution:

#Write your function here
def double_index(lst, index):
lst.insert(index, lst[index] * 2)
return lst
return lst

#Uncomment the line below when your function is done
print(double_index([3, 8, -10, 12], 2))

I believe this challenge is looking for you to double an existing value at a specific index, .insert is for adding new elements to a list which I think is different to what the challenge is after here.

If you’re asked to return a new list instead of modifying an existing one then it would be worthwhile also following that step.

1 Like

What’s more, this challenge comes up early enough in the course that many learners are still grappling with index, and long before any discussion about exception handling. Your solution jumps way ahead of the queue. Suggest create the naive solution expected and not complicate matters with code that you might not fully understand. This really is a simple problem. Keep your solution simple.

1 Like

I just thought except was good to handle the erroneous case that will arise if the index is not in the range.

1 Like

There is no fault in that reasoning, although it is over the boards for this exercise. Be sure you can ideate and write the naive form of the solution.

  • check that index is in range
  • mutate the value at that index if it is in range

@tgrtim explained why .insert() is not applicable in this case.

Okay thanks!

Also worth mentioning is that you said exception handling hasn’t been mentioned in the course at this point, but this is untrue. We have been introduced to break, try, except and continue at this point.

Ah, I see. Thanks for correcting my assumption. Have lost track of what comes up where and when.

def double_index(lst, index):
        lst[index] *= 2
    except IndexError:
        print ("Index is out of bounds.")
    return lst

Ah I like that, we’ve only been introduced to except though at this point. When we’ve used it it is just “except:”. I haven’t seen it used the way you have there with ‘IndexError’ written after it.

1 Like

That is the only exception that will get caught. If we pass in something that is not iterable, or that is immutable, Python will raise its own fatal error such as ValueError or TypeError. We’re only interested in the one type of error, IndexError. Be sure to do some extra reading to get yourself up to steam on this concept.

Rule One: only wrap code that may raise an exception in the try block. All the rest can follow after the except.

1 Like

Why not just use except here though to capture ALL errors, perhaps by printing an error message for the user. Why only specify the index error case?

We can specify any number of exceptions that we wish to handle, using multiple except blocks.

    except TypeError:
        print ("Object is not a list.")

Catching all the exceptions means we might not have a valid error message. Personally, I like to let Python handle all the errors while any are still possible so we can identify and except them in turn.