0

I want to move to the top of a set of nested loops as I am removing items from a list to run arithmetic on. So when I use break it ends all the loops, and I tried break/continue but to no luck. Is there a way to break out of the inner loops and have it start from the top loop?

def postfix_eval(chaine):
chaine, cumulator, basedict, zed = chaine.split(), 0, [], 0
for x in chaine:
    if x.isdigit():
        basedict.append(int(x))
        chaine.remove(x)
    for y in chaine:
        if y.isdigit():
            basedict.append(int(y))
            chaine.remove(y)
            print("chaine at y" , chaine) 
            for zed in chaine:
                if zed == "-" or zed == "+" or zed == "*" or zed == "/":
                
                    chaine.remove(str(zed))
                    print("chaine at zed", chaine)
                    operators = {'+': int(x)+int(y) , '-': int(x)-int(y), '/': 
                                int(x)+int(y), '*':int(x) * int(y)}
                    cumulator += operators[zed]
                    break
                continue
            continue
        
                            
    return cumulator
        

1 Answer 1

0

In python, there is a concept for/else loops. So imagine having a simple code like:

for x in a:
    for y in b:
        ...
        if (...):
            break
    else:
        ...

Your program will go into the else block only if the loop was not interrupted by the break. If not clear maybe look it up.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks that helps, I also found this link: stackoverflow.com/questions/40602620/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.