嵌套以循环和休息
为什么以下代码不打印任何东西?满足条件的A和B的第一组值为A = 1和B = 4。但是,我看不到它正在打印。
def fun():
for a in range(5):
for b in range(5):
if a + b == 5:
print(a,b)
break
break
fun()
Why does the following code not print anything? The first set of values of a and b that satisfies the condition is a = 1 and b = 4. However, I don't see that being printed.
def fun():
for a in range(5):
for b in range(5):
if a + b == 5:
print(a,b)
break
break
fun()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您有一个额外的
中断
,因为第一个将终止最内向的循环,从而可以增加您想要的最外层(A)。折断两次终止整个执行,因此您的代码应该看起来像这样:edit
,因为您想显示第一组值,而不是
break
它是return return < /代码>终止整个执行所需的。
You have an extra
break
, as the first one will terminate the innermost loop, allowing to increment the outermost (value of a) which is what you want. Breaking twice terminates the whole execution so your code should look like this:EDIT
Since you want to display the first set of values then instead of
break
it's areturn
that is needed to terminate the whole execution.