Scope of python variable in for loop

前端 未结 10 1960
一整个雨季
一整个雨季 2020-11-22 15:16

Heres the python code im having problems with:

for i in range (0,10):
    if i==5:
        i+=3
    print i

I expected the output to be:

10条回答
  •  一向
    一向 (楼主)
    2020-11-22 15:58

    Python's for loop simply loops over the provided sequence of values — think of it as "foreach". For this reason, modifying the variable has no effect on loop execution.

    This is well described in the tutorial.

提交回复
热议问题