[英]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.我有一个基于计数值的 while 循环。 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.当我的内部循环运行时,计数会增加,但如果计数超过目标 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.期望的结果:如果计数<=50,那么我希望循环中断到最外层循环并将手套循环设置为列表中的下一个值并继续。
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
You can use a flag = 0
at the starting of your outermost loop.您可以在最外层循环的开头使用flag = 0
。 Set flag = 1
when count exceeds 50. Now add if flag == 1 then break
after every loop you want to break.当计数超过 50 时设置flag = 1
现在添加if flag == 1 then break
在每个要中断的循环之后中断。 I hope this is helpful.我希望这是有帮助的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.