1

Is there a way to get a for loop to start again at any point within the loop? Say line has a length of three. If the if statement is satisfied and my index increments, I want everything to start checking again at the first value of line. (Note this whole thing is part of a while True loop).

for line in library:
        if message[index:index + len(line)-2] == line[2:]:
            ans += line[0]
            index += len(line)-2 + 1
K. Carpenter
  • 19
  • 1
  • 3
  • I don't get what you are asking here ... – ᴀʀᴍᴀɴ Feb 25 '17 at 20:59
  • @Arman so line contains values that I want to check substring by substring in message. Currently, when I find a match and increment the index to see if the next bit also has a match in line, it only starts at the next element of line (and skips over the first bits). So even if there is a match, I won't get it. I wondered if I could restart the for loop to the first element of line after index increments? – K. Carpenter Feb 25 '17 at 21:02
  • there are many ways to do this, have a look into these: http://stackoverflow.com/questions/492860/python-restarting-a-loop http://stackoverflow.com/questions/3704918/python-way-to-restart-a-for-loop-similar-to-continue-for-while-loops – thanasisp Feb 25 '17 at 21:20

2 Answers2

0

I think you need another while True above the for loop. For example, this is a continuous loop unless there is a break condition

x = [1, 2, 3, 4, 5, 6]
restart = True
while restart:
    for i in x:
        # add any exit condition!
        # if foo == bar:
        #   restart = False
        #   break
        if i == value:  # add your message condition
           break

You could replace x with library in your case and i with line. Then instead if i == 4 you add your break condition that resets the loop and if you do have an actual break condition, you set the restart flag to False

Another option would be using a loop counter like n = 2 and use while n < 2: and then set n += 1 whenever your condition mets or n = 0 whenever you want to reset.

Laurent LAPORTE
  • 21,958
  • 6
  • 58
  • 103
Alex C.
  • 31
  • 7
  • you need an StopIteration catch otherwise it will restart once the list is ended as well – Naib Feb 25 '17 at 21:30
0

If I'm reading it right you want to entirely restart the loop when you find a match. Easiest way to do this is by breaking out of the for loop as you stated you have a while loop above it which remains True and will restart the for loop:

for line in library:
        if message[index:index + len(line)-2] == line[2:]:
            ans += line[0]
            index += len(line)-2 + 1
            break
Koen vd H
  • 101
  • 3
  • How would I break out completely? Yes I have a while loop around all of this, but I have more code beneath this that I want to be able to reach. When I break now I just leave the for loop, but the break brings me back into the for. How do I leave the for and the while to get to the next bits of code? Thanks :) – K. Carpenter Feb 25 '17 at 22:24
  • update: I got it :) made a string equal to a word and then checked if the word equaled that to break out of it. thanks everyone! xx – K. Carpenter Feb 25 '17 at 23:05
  • @K.Carpenter Good to hear! Although, if you simply want to exit the while loop when you have reached the end of the for loop(including restarts) you could just add an else statement on the same indentation as the for loop in which you turn the while loop condition to False. This else statement will be reached when the for loop ends. – Koen vd H Feb 26 '17 at 08:29