Home > Mobile >  How can I store a variable in a list number?
How can I store a variable in a list number?

Time:10-26

I have a piece of code which uses a counter to move through a list but the problem is that when I do counter 1 it does not store in the variable counter.

code:

list = [1,2,3,4,5]
counter = 0
b = list[counter] list[counter 1]
print(counter)

I expect this to return 1 since I added 1 to the counter but instead it returns 0 as if i had not add anything.

CodePudding user response:

As joshmeranda said, counter 1 does not change the counter variable.

You'd probably have to separately increment the counter variable, like:

list = [1,2,3,4,5]
counter = 0
b = list[counter] list[counter 1]
counter  = 1 #variable  = 1 is syntastic sugar for variable = variable   1
print(counter)
  • Related