python - 内部 While 循环只运行一次 - Python
问题描述
我正在为程序使用嵌套的 While 循环,我注意到内部的 while 循环只运行一次。
i = 0
j = 0
while i < 5:
while j < 5:
print('Inner While Loop')
j += 1
print('Outer While Loop')
i = i+1
输出 :
Inner While Loop
Inner While Loop
Inner While Loop
Inner While Loop
Inner While Loop
Outer While Loop
Outer While Loop
Outer While Loop
Outer While Loop
Outer While Loop
我想要的是当外部循环再次启动时内部循环也再次运行。
解决方案
我认为您的问题是您没有重置 j 变量。所以下一次外循环执行时,j 仍然是 5,你跳过内循环。例如,如果您更改代码以在第一个 while 循环中初始化 j ,我相信一切都应该正常工作:
i = 0
while i < 5:
j = 0 # Moved to inside the while loop
while j < 5:
print('Inner While Loop')
j += 1
print('Outer While Loop')
i = i+1
推荐阅读
- javascript - VM299:1 未捕获的语法错误:JSON.parse 中位置 0 处的 JSON 中的意外标记 u(
) - sql - psql:关系不存在
- mongodb - MongoDB 平衡器未启动
- node.js - 嵌套异步等待与循环结合
- macos - 什么是 NEIKEv2Provider,为什么它阻止我访问互联网?
- azure-cognitive-services - 表单识别器中的不同表单模板
- azure - Azure VM 出现预配失败问题
- excel - 将鼠标悬停在 Excel 应用程序上时出现隐藏的用户窗体
- maven - 如何在 Pom.xml 中为 Firefox 和 Internet Explorer 添加 maven 依赖项?
- html - 如何减少压痕
- UILabel html属性文本中的标签?