Python Lists Double index- What's my "else" problem?

Hi everyone! I have look for other discussions, but I have not found this problem, I hope to not ask something already answered.
So, the exercise states: Create a function named double_index that has two parameters: a list named lst and a single number named index .
The function should return a new list where all elements are the same as in lst except for the element at index . The element at index should be double the value of the element at index of the original lst .
If index is not a valid index, the function should return the original list.
After some difficulties in the understanding what I was supposed to do, I came out with this code:

#Write your function here
def double_index(lst,index):
   ind= lst[index] *2 #this is the double index
   if ind<=len(lst):
     lst = lst[0:index] + lst[index+1:]
     lst.append(ind)
     return lst
   else:           #elif ind>len(lst): 
    return lst

I have tried it, and it works! :blush:
My problem is that, when I check my answer, this is what it came out: “Make sure to define what should happen if index is too big!”.
I have also tried the elif in the comment, but the result is the same. I have checked and if I put an invalid index, it returns the original list (as it is stated in bold). So I am a little bit lost, am I misunderstanding the challenge? Am I missing something? Am I crazy? :joy:
Please, help this poor soul! :stuck_out_tongue_closed_eyes:

The wording of this exercise has most everybody thinking it is the index that is doubled. Gawd I wish they would change the name of the function. It should be,

double_value_at_index

We are not doubling the index.

A valid index is one that is in range.

if index < len(lst):   #  it is valid

There is no list slicing (has that even come up yet?), and no need to append anything. Validate the index, locate the value at that valid index, extract it and double the value, then put it right back where you got it from.

2 Likes

Thank you so much. Now it all make sense! Finally I managed to put in the right solution…

1 Like