与python嵌套流控制混淆

2024-05-13 11:52:11 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我有一个基于计数值的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

Tags: thelooptrueiscountwithokbe
1条回答
网友
1楼 · 发布于 2024-05-13 11:52:11

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

相关问题 更多 >