与 python 嵌套流控制混淆

confusion with python nested flow control

我有一个流程控制问题,我之前在一个问题上得到了一些很好的建议,现在需要使我的简化版本更接近实际问题。

我有一个基于计数值的 while 循环。当我的内部循环是 运行 时,计数会增加,但如果计数超过 50 的目标,我想跳出除最外层循环之外的所有循环。

代码:

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') 

当前结果:

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

想要的结果: 如果计数 <=50 那么我希望循环突破到最外层循环并将手套循环设置为列表中的下一个值并继续。

修改代码以添加标志:

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') 

带有我放错位置的标志的结果:

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

您可以在最外层循环的开头使用 flag = 0。 当计数超过 50 时设置 flag = 1。 现在在每个要中断的循环之后添加 if flag == 1 then break 。 希望对您有所帮助。