How do I run loop again with a change based on 'if condition' in Python 3?

How do I run loop again with a change based on 'if condition' in Python 3?
0

#1

let us say.

a = 1
b = 10
for i in range(a, b):
print(i)
if i == 5:
a +=1 # and run loop again.

How would I increase a by 1 and run the loop again?
Is there any way?


#2

You could use two loops, but think twice about what you want to achieve, as I don’t think any of this will do what you want.

Here’s something to think about:

>>> a=range(1,6)
>>> a
[1, 2, 3, 4, 5]
>>> for i in a:
...     print a[i-1:]
... 
[1, 2, 3, 4, 5]
[2, 3, 4, 5]
[3, 4, 5]
[4, 5]
[5]

#3

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.