首页 > 解决方案 > 在过程 i while 循环之后添加增量有什么区别

问题描述

这两个代码有什么区别,我在第一个片段上得到无限循环

while (e <5):
   print("hello)
    e = e+1

第二个片段中的常规 while 循环

 while (e <5):
   e = e+1
   print("hello)

标签: pythonloopsoopwhile-loop

解决方案


两个代码片段都按预期工作。您可能在第一个代码段中有缩进问题。

e = 0
while e < 5:
   print("hello")
   e = e + 1

e = 0
while e < 5:
   e = e + 1
   print("hello")

推荐阅读