【问题标题】:Why doesn't "while True" increments "i" continuously in python? [closed]为什么“while True”不会在python中连续增加“i”? [关闭]
【发布时间】:2017-03-26 20:50:07
【问题描述】:

我编写了一个代码,使得 'i' 的值应该增加,直到外部 while 循环中断(图像中显示的代码结构)。

但是,这不会增加“i”的值,它总是被打印为“1”。

这是我写的代码的结构:

while True:
  statement1
  statement2
  ...
  ...
  i=0
  i=i+1
  print(i)
  if some_condition=True:
    break;

如果我想不断增加“i”的值直到外部while循环中断,我应该怎么做?

【问题讨论】:

  • 请发布一个可重现的示例
  • 请不要在其他网站上链接到您的代码。
  • 并使用代码块,而不是图像

标签: python python-2.7


【解决方案1】:

您总是在 while 循环中设置 i=0。在 while 语句之前移动 i=0。

i=0
while True:
  statement1
  statement2
  ...
  ...
  i=i+1
  print(i)
  if some_condition=True:
    break;

【讨论】:

    【解决方案2】:

    因为您在每次迭代中都设置了i=0。将 i=0 放在您的 while 循环之前。

    【讨论】:

      猜你喜欢
      • 2011-10-06
      • 2015-06-21
      • 1970-01-01
      • 2018-09-10
      • 2020-11-25
      • 1970-01-01
      • 2014-03-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多