首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >与python嵌套流控制混淆

与python嵌套流控制混淆
EN

Stack Overflow用户
提问于 2020-05-07 18:46:39
回答 1查看 74关注 0票数 0

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

我有一个基于count值的while循环。当我的内部循环运行时,计数是递增的,但如果计数超过50的目标,我想要中断除最外面的循环之外的所有循环。

代码:

代码语言:javascript
复制
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') 

当前结果:

代码语言:javascript
复制
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,那么我希望循环中断到最外层的循环,并将手套循环设置为列表中的下一个值,然后继续。

修改后的代码添加了标志:

代码语言:javascript
复制
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') 

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

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-07 19:02:59

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61655878

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档