Index not updating (again)

I’m making a calculator program and I’m currently on implementing negatives. So if I input 1--1--1 into the program, it’ll first split it into a series of operations and integers, so that will turn into [1, '-', '-', 1, '-', '-', 1] . Then the functions I’m writing now are supposed to put the negatives where they go, so the preferred output in this case would be [1, '-', -1, '-', -1] . Here are the functions so far:

def join_neg(lst):
    tempstr = ''
    tempint = 0
    neg_ndx = 256000
    for num in lst:
        if num == '-':
            neg_ndx = lst.index(num)
            break

    if neg_ndx != 256000:
        neg = lst[neg_ndx]
        potential_int = lst[neg_ndx + 1]
        if isinstance(potential_int, int):
            if potential_int > 0:
                tempstr = neg + str(potential_int)
                del lst[neg_ndx:neg_ndx + 2]
                tempint = int(tempstr)
                lst.insert(neg_ndx, tempint)
            elif potential_int <= 0:
                print(neg_ndx)
                return join_neg_two(lst, neg_ndx)
        elif isinstance(potential_int, str):
            neg = lst[neg_ndx + 1]
            potential_int = lst[neg_ndx + 2]
            tempstr = neg + str(potential_int)
            del lst[neg_ndx + 1:neg_ndx + 3]
            tempint = int(tempstr)
            lst.insert(neg_ndx + 1, tempint)

        return join_neg(lst)
    elif neg_ndx == 256000:
        return lst


def join_neg_two(lst, ndx):
    tempstr = ''
    tempint = 0
    neg_ndx = 256000
    ndx += 1
    print(ndx)
    for num in lst[ndx:]:
        if num == '-':
            neg_ndx = lst.index(num)
            break
    print(neg_ndx)
    if neg_ndx != 256000:
        neg = lst[neg_ndx]
        potential_int = lst[neg_ndx + 1]
        if isinstance(potential_int, int):
            if potential_int > 0:
                tempstr = neg + str(potential_int)
                del lst[neg_ndx:neg_ndx + 2]
                tempint = int(tempstr)
                lst.insert(neg_ndx, tempint)
            elif potential_int <= 0:
                return lst
        elif isinstance(potential_int, str):
            neg = lst[neg_ndx + 1]
            potential_int = lst[neg_ndx + 2]
            tempstr = neg + str(potential_int)
            del lst[neg_ndx + 1:neg_ndx + 3]
            tempint = int(tempstr)
            lst.insert(neg_ndx + 1, tempint)

        return join_neg(lst)
    elif neg_ndx == 256000:
        return lst

One thing to note is that the functions are supposed to do this with any minus it finds, provided the next index contains an integer, otherwise it continues on. So [1, '-', 1] should output [1, -1] . Another thing I think I should address is the fact that the variable neg_ndx is assigned to the value 256000 , this is how the program knows that it’s finished combining all the negatives, and can move onto the next set of functions. The issue I’m currently having is that in the join_neg_two function, (using the example [1, '-', '-', 1, '-', '-', 1] which will be passed to this function looking like this: [1, '-', -1, '-', '-', 1] ) the for loop should start iterating at the -1 object in the list, so (I think) the variable neg_ndx should become 3 which will then be passed to the rest of the function, but it doesn’t. It gets turned into 1 which messes up the rest of the function. Is there something wrong with the way I’m iterating through the list starting at a different index other than 0 or is it something else?

Last thing, I seem to do a bad job formatting code since the last time I asked a question I got a lot of people telling me that variables, style, etc. were pretty bad, so if you find that my style is incomprehensible and need clarification or anything worse than what you’d expect from a beginner, please let me know. Although I’d rather the discussion not focus completely around stylistic issues, as I still need help with the core problem.

Maybe I’m missing something, but the way I see this is that, at line 43 (neg_ndx = lst.index(num), on first pass, neg_ndx will hold the position of the first occurrence of the specified value, in this case '-', the first occurrence being position 1, not 3.

Is it retaining that from the previous function? Since the for loop starts at an index past 1 (in this case 2), I didn’t think it the loop would pass over index of 1.

Can I ask you what IDE you are using?

Website called Replit.com. It uses Python 3.8.2.

Okay. Maybe you have your reasons for that, but I’m using Wing Personal V8.1.1.0 which has helped me understand bugs that I’ve had to chase down as one can single step through ones code – you may want to give it a try.

I’m driving your code block with this simple command:
print(join_neg_two([1, '-', -1, '-', '-', 1],0)) as you said

So I’ve not looked into the previous function.

You should probably drive it as this:
join_neg_two([1, '-', -1, '-', '-', 1], 1)
This is what would be run from the previous function.
As for my reasoning for that, it’s because I’m still in high school and like to code in school in my free time with my school issued Chromebook. But they have their limitations and stuff so I can’t really use linux for coding, nor can I download other software, so I opt for that free online IDE. At home I use my personal computer and generally use either Pycharm or Visual Studios, but I find it tedious and annoying to have to manually copy my code, so I just use replit at home as well.
I’ve never heard of Wing Personal, can you link a download for it?

Okay, ndx now gets incremented to 2 at line 39 (seen in the output at line 40). Then we start the for loop at line 41. At line 42 num is assigned the value -1, so we jump back to line 41, where num is assigned the value '-', now we hit line 43, where neg_ndx is assigned lst.index(num), which will be the first occurrence in your list.

Ok, when I loop through a list started at a specific index, that specific index gets interpreted as the first index of the list it’s looping through?

What you’re doing is looking for a ‘-’ starting at index ‘-’ (position 1), which is at position 1

It should be starting at position 2. That’s what the ndx += 1 does in the join_neg_two function.

You need to increment ndx inside the for loop.

edit: as above, ndx is not getting incremented, aside from line 39

I’m not sure if this will help, but if what you’re trying to do here, is display the operation that your calculator is doing, would it not be better to convert the output (the displayed output) to a string? You could then use a string find() method to search for the positions of the - symbols.

Well what I was hoping this would do is run the entire join_neg function again but instead of iterating through the list starting at 0, it starts at 2 in this case. I don’t think the find() method works in this case since it returns the first occurrence of a given value and when the input is [1, '-', -1, '-', '-', 1] or other, I need a way around the first occurrence of -.

Would this kind of thing help?

display = ['1', '-', '-1', '-', '-', '1']

for position, symbol, in enumerate(display):
    if symbol == '-':
        print(position)

The output being:

1
3
4