what happened when using the same variable in two layer loops in python?

前端 未结 6 1662
星月不相逢
星月不相逢 2021-01-12 14:18

I test the following code:

for i in range(3):
    for i in range(3,5):
        print \"inner i: %d\"%(i)
    print \"outer i: %d\"%(i)

and

6条回答
  •  清酒与你
    2021-01-12 14:51

    Its the same i being used in both loops. When the outer loops gets a chance to print i, it will always have the last assigned value from the inner loop.

提交回复
热议问题