Help | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Help

for i in range(0,2): for i in range(0,4): print(i) Why do I get back 0 1 2 3 0 1 2 3 from the code and not 0 1 2 0 1 2 3 ? Thanks

19th Nov 2018, 10:15 PM
Pauline
Pauline - avatar
2 Answers
+ 2
I wouldn't recommend re-using the same variable "i" this way. This can only lead to bugs and unexpected behaviour. In any case The inner loop prints "0 1 2 3" and it is executed twice because of the outer loop. That is why you get this output
19th Nov 2018, 10:48 PM
bullion
+ 1
The code runs through to the inner loop and finishes it. Then it jumps back to the outer loop and starts the whole thing once more.
20th Nov 2018, 12:01 AM
HonFu
HonFu - avatar