Screen Link:
My Code:
values = [16, 1, 7, 2, 19, 12, 5, 20, 2, 10, 10, 14, 17, 14, 1, 16, 19, 7, 9, 19]
for num in values:
if j is not int:
j = 0
j = values.index(num,j)
values[j] = num + 1
print(values)
What I expected to happen: Loop through set of values, +1 to each value. The actual solution to the problem builds a new list using range(len(values)), I wanted to try and do it with just the values list.
The output works if j = 0 is declared outside the loop and the if: is removed from the loop, but then I have a variable floating around outside the loop which I don’t like:
j=0
for num in values:
j = values.index(num,j)
values[j] = num + 1
What actually happened: +2 on some values, +1 or 0 on others.
[18, 3, 8, 3, 21, 13, 6, 20, 2, 11, 11, 15, 17, 15, 2, 17, 20, 8, 10, 20]
Any advice? I think I’m struggling to get python to create j = 0 if j doesn’t already exist, but then I’m not really sure why the output is the way that it is.