-1

I have a code that looks like this:

some_list = []
try:
    while True:
        ... code that uses the list ...
except KeyboardInterrupt:
    ... code that modifies the list ...

after handling this exception, how do I return to executing this loop without nesting the loop?

Krzysiek127
  • 29
  • 1
  • 2
  • You can not return to the loop after catching the exception. Instead use the `try-catch` block within the loop so you can continue iterating after an exception has been caught. – dimitar.d Jan 08 '21 at 12:01

1 Answers1

-1

you can simply re-structure your code to swap the loop with the try/catch

See this as example:

from time import sleep

some_list = []

while True:
    try:
        print(some_list)
        sleep(1)
    except KeyboardInterrupt:
        some_list.append("1")
Tibebes. M
  • 6,940
  • 5
  • 15
  • 36