Why doesn't this loop break?

前端 未结 6 1460
太阳男子
太阳男子 2021-01-16 00:01

Python noob; please explain why this loop doesn\'t exit.

for i in range(0,10):
  print \"Hello, World!\"
  if i == 5: i = 15
  print i
next

6条回答
  •  难免孤独
    2021-01-16 00:15

    Because what you have done with range(0,10) is created an array of 10 elements like so:

    1, 2, 3, 4, 5, 6, 7, 8, 9, 10
    

    and you are going through each one.

    In other programming languages, you are doing what is called a foreach loop.

    Otherwise, do it another way.

提交回复
热议问题