简体   繁体   中英

Go back to a broken loop in python

There is 2 different options for leaving a loop in python. continue, which brings you back to the beginning of the loop, and break which is like a light switch and it cuts off the loop for the rest of the time that the script runs. My problem is I have a while True loop, and I want to be able to break out of it and then return to it at a later time in the code. Is this possible, if so, how can I do it? So far I have something like this:

while True
    if #condition:
        #do something
    else:
        #do something
        break
while True
    if #condition:
        #do something
    else:
        break
#code that returns it to the first loop

Is there a way to do this, like an unbreak or something? Sorry as I am new to programming, It is my first year in Computer Science and I am just starting to learn python.

You could put the first loop in a function.

def first_loop():
    while True:
    if something:
        # do something
    else:
        break

# call loop for first time
first_loop()

while True:
    if something:
        #do something
    else:
        break

# return back to the loop
first_loop()

You can't break out of a loop and then return to it.

Well ... you possibly can, but that will require the use of the infamous 'goto' keyword ... you should avoid that at all costs.

Typically, when you want to do something, stop doing it, and then do it again later, you put that 'something' into a function or procedure. Then every time you want to do 'something' you call, or run, the function.

def doSomething():
    while True
        if #condition:
            #do something
        else:
            #do something
            break

def doSomethingElse():
    while True
        if #condition:
            #do something
        else:
            break

def doOneMoreThing():
    while True
        if #condition:
            #do something
        else:
            break

then you can repeat the somethings in any order you want:

doSomething()
doSomethingElse()
doSomething()
doSomething()
doOneMoreThing()

You should simply wrap the loops in functions:

def loop1():

    while True
        if #condition:
            #do something
        else:
            #do something
            break

def loop2():

    while True
        if #condition:
            #do something
        else:
            break

loop1()
loop2()
loop1()

Just wrap another loop:

while True  # outer loop
    while True  # first loop
        if #condition:
            #do something
        else:
            #do something
            break
    while True   # second loop
        if #condition:
            #do something
        else:
            break
   # break from outer loop 
   # when you are done returning to the first loop

I think you should do vice versa. There's no need to break an infinite loop. Just make some functions and switch between them

def function1():
   do something

def function2():
   do something else

while True:
    if something:
        function1()
    else:
        function2()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM