Home > database >  I want to move to the top of nested loops ~ will break, or continue work?
I want to move to the top of nested loops ~ will break, or continue work?

Time:11-13

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
        

CodePudding user response:

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.

  • Related