Why my answer is wrong?

I hope the explanation will be clear. Thank you

What happens when you google the error? TypeError: 'int' object is not subscriptable?

What int object is the error referring to?
What is subscripting and where are you using it?

Then you can zoom in to the problem.

You can print value before entering the inner loop if you’re ensure what the outer loop is producing

1 Like

I haven’t still found where the problem is in the code

@Maho: On the line value[i] = value[reverse], reverse is initialized inside the loop, so it will always be -1 in each iteration of the loop. Also not sure why you needed a nested loop for this. Mind explaining your thought process?

I have tried to think about another solution so I wrote this code.
On the line value[i] = value[reverse], reverse is initialized inside the loop, so it will always be -1 in each iteration of the loop, But I’ve written reverse=reverse-1 so the value of reverse must change at the end of each loop.

This value changes, that is correct. But when after it checks the condition using the range() function, reverse gets initialised to -1 again.

1 Like

I am very sorry but I don’t understand.
I think there is no relation between range and reverse, the relation is between range and i.

From your earlier question I can see that you have understood about re-assigning new values to the variable.

But here you have to look at the sequence of code execution. Python is executed from top to bottom, line by line, with some exceptions if there is function definition, if condition etc.

So here, in each new iteration of the inner loop, the first line of the inner loop gets executed which is
reverse = 0
and assigns the value 0 to reverse and then the next line gets executed
reverse = 1
and assigns the value 1 to reverse.
All the assignment happened to reverse such as reverse = reverse-1 in the earlier execution of the loop is overwritten when the line value[i]= value[reverse]

So maybe you need to look into a different logic to get the required answer.

1 Like

For the first loop: i = 0, reverse = -1, value[0] = -1 , reverse = -1 -1=-2 and so on, this is what I expect, I don’t know where the mistake is.


There are some issues to be fixed in your code.

First of all, you have to create an empty list first, which you will further fulfill with the reversed values of your existing list:

reversed_values = [] 

Second, you definitely have to assign your reverse outside (before) the loop and then, inside the loop, to decrease it by 1 at each iteration, in the end of the loop. And yes, it should initially start with -1 (the index of the last item in the list):

reverse = -1
for i in range(len(values)):

Next, there are 3 ways to add new values to a list:

  • append() - append one value at the very end of the list,
  • expand() - append several values at the very end of the list,
  • insert() - insert a value inside the list at a specific index.

Obviously, in our case, we want to append a new item (wich is actually one of the items of the list values) at the very end of the list at each iteration, so we use the first approach:


Finally, we have to decrease the value of reverse inside the loop and at the end of the loop, for further using this updated value for the next iteration:

reverse = reverse - 1
1 Like