Windy71
Windy71

Reputation: 909

confusion with python nested flow control

I have a flow control problem, I got some good advice earlier on a question and now need to make my simplified version closer to the actual problem.

I have a while loop based on a count value. When my inner loop is running the count is incremented but I would like to break out of all the loops except for the outermost loop if the count exceeds the target of 50.

code:

smurf = True
print('smurf status: ', smurf)
jackets = list(range(5))
print(jackets)
gloves = [7,8,9]

for g in gloves:
    print('The glove is :',g)
    smurf = True
    whitesocks = True
    count = 10
    while count <= 50:
        for j in jackets:
            print('\tjacket = ', j, '\n')

            while smurf is True:
                print('count =', count)
                print('\t\twhile true jacket is :', j)
                if j == jackets[-3]:
                    smurf = False
                    print('\t\tsmurf is FALSIOOOO SO BREAK')
                    break #to escape the while loop
                print('\t\tsmurf with jacket ',j, ' be ok')
                j += 1
                count += 33
            print('doo dah')
            break # To escape the for-loop

        break # i think this is to escape while count
    print('After the jacket loop')
    #break
print('END OF SMURFGATE') 

current results:

smurf status:  True
[0, 1, 2, 3, 4]
The glove is : 7
        jacket =  0 

count = 10
                while true jacket is : 0
                smurf with jacket  0  be ok
count = 43
                while true jacket is : 1
                smurf with jacket  1  be ok
count = 76
                while true jacket is : 2
                smurf is FALSIOOOO SO BREAK
doo dah
After the jacket loop
The glove is : 8
        jacket =  0 

count = 10
                while true jacket is : 0
                smurf with jacket  0  be ok
count = 43
                while true jacket is : 1
                smurf with jacket  1  be ok
count = 76
                while true jacket is : 2
                smurf is FALSIOOOO SO BREAK
doo dah
After the jacket loop
The glove is : 9
        jacket =  0 

count = 10
                while true jacket is : 0
                smurf with jacket  0  be ok
count = 43
                while true jacket is : 1
                smurf with jacket  1  be ok
count = 76
                while true jacket is : 2
                smurf is FALSIOOOO SO BREAK
doo dah
After the jacket loop
END OF SMURFGATE

desired results: If the count is <=50 then I would like the loops to break out to the outermost loop and set the glove loop to the next value in the list and carry on.

Code amended to add flags:

smurf = True
print('smurf status: ', smurf)
jackets = list(range(5))
print(jackets)
gloves = [7,8,9]

flag = 0
for g in gloves:

    print('The glove is :',g)
    smurf = True
    whitesocks = True
    count = 10
    while count <= 50:
        for j in jackets:
            print('\tjacket = ', j, '\n')

            while smurf is True:
                print('count =', count)
                print('\t\twhile true jacket is :', j)
                if j == jackets[-3]:
                    smurf = False
                    print('\t\tsmurf is FALSIOOOO SO BREAK')
                    if flag == 1:
                        break
                    break #to escape the while loop
                print('\t\tsmurf with jacket ',j, ' be ok')
                j += 1
                count += 33
                if count >= 50:
                    flag = 1
            print('doo dah')
            if flag == 1:
                break
            break # To escape the for-loop
        if flag ==1:
            break
        break # i think this is to escape while count
    print('After the jacket loop')
    #break
print('END OF SMURFGATE') 

results with flags that I have misplaced:

smurf status:  True
[0, 1, 2, 3, 4]
The glove is : 7
        jacket =  0 

count = 10
                while true jacket is : 0
                smurf with jacket  0  be ok
count = 43
                while true jacket is : 1
                smurf with jacket  1  be ok
count = 76
                while true jacket is : 2
                smurf is FALSIOOOO SO BREAK
doo dah
After the jacket loop
The glove is : 8
        jacket =  0 

count = 10
                while true jacket is : 0
                smurf with jacket  0  be ok
count = 43
                while true jacket is : 1
                smurf with jacket  1  be ok
count = 76
                while true jacket is : 2
                smurf is FALSIOOOO SO BREAK
doo dah
After the jacket loop
The glove is : 9
        jacket =  0 

count = 10
                while true jacket is : 0
                smurf with jacket  0  be ok
count = 43
                while true jacket is : 1
                smurf with jacket  1  be ok
count = 76
                while true jacket is : 2
                smurf is FALSIOOOO SO BREAK
doo dah
After the jacket loop
END OF SMURFGATE

Upvotes: 0

Views: 104

Answers (1)

Harirai
Harirai

Reputation: 164

You can use a flag = 0 at the starting of your outermost loop. Set flag = 1 when count exceeds 50. Now add if flag == 1 then break after every loop you want to break. I hope this is helpful.

Upvotes: 2

Related Questions