I’m making a little function that’s supposed to consolidate negative numbers and I’m having trouble with the indexes, here’s the relevant parts of the function:
if str_amnt >= int_amnt:
for i in lst:
if f_op == False:
if isinstance(i, str) == True:
f_op = True
f_op_ndx = lst.index(i)
print(f_op_ndx)
continue
if f_op == True:
if isinstance(i, int):
f_op = False
continue
elif i == '-':
if lst[f_op_ndx] != '-':
neg_indx = lst.index(i)
int_indx = neg_indx + 1
elif lst[f_op_ndx] == '-':
neg_indx = lst.index(i) + 1
int_indx = neg_indx + 1
numstr = str(lst[neg_indx]) + str(lst[int_indx])
numint = numstr
del lst[neg_indx:int_indx + 1]
lst.insert(neg_indx, numint)
return lst
elif int_amnt > str_amnt:
return lst
So the problem currently is if I input [1, '-', 1, '-', '-', 1]
it returns [1, '-', '1-', '-', 1]
instead of the desired output. By printing stuff out and stuff, I found that the variable f_op_ndx
never changes index past the first iteration, I don’t understand why because when it gets to the 4th iteration of the for loop I think it should change the index to 3, but running the code keeps it stagnant at one. What’s happening?