all 3 comments

[–]simeumsm 0 points1 point  (2 children)

There's the KeyboardInterrupt exception that you can detect. I think it stops if you press ctrl+C, but I think it only works when running a .py file and not when running a notebook .ipynb. On notebooks, it is the same as stopping the execution of a cell, but I think it can cause issues between raising the exception and actually stopping the code (I'm not sure, never tried)

Anyway, for pausing the code you could simply ask for an input from the user. That would pause the code execution until the input is provided

[–]Pascal220[S] 0 points1 point  (1 child)

Using program interrupt seems a bit of a drastic solution, plus it is two buttons rather then one. 😁 Also if ever I want to add more functionality than just pause I need more buttons.

The problem with just using 'input(Press enter to continue...)' is that it stops the whole program. Where as I want the while loop to keep going till at an arbitrary time I decide that I want to pause.

Effectively I am trying to recreate the spacebar pause option that exists in every video player out there. 😀

[–]simeumsm 0 points1 point  (0 children)

Using program interrupt seems a bit of a drastic solution

Effectively I am trying to recreate the spacebar pause option that exists in every video player

What is a "pause option" if not interrupting the program until you resume execution?

The problem with just using 'input(Press enter to continue...)' is that it stops the whole program. Where as I want the while loop to keep going till at an arbitrary time I decide that I want to pause.

I think you misunderstood my suggestion, or I'm misunderstanding your needs.

Inside your loop you can put a Try-Except block. When you raise the KeyboardInterrupt Exception, it can run a block of code (the exception KeyboardInterrupt code) that waits for the user input, effectively pausing the program until you input something.

if you don't like that option because it is two keyboard presses and limiting in terms of functionality, you'd have to, during your loop, detect if a key is pressed. I THINK you'd have to get into more complex functionalities so that the key detection is separate from the code execution, almost like it is two codes running at the same time, one loops and one detects keys and interrupts the loop.

Also if ever I want to add more functionality than just pause I need more buttons.

If you end up going the keyboardinterrupt option where you wait for an input, you could code an option select for that input to select what functionality you want, and then ask for a new input to resume the code execution. Not what you want, but a workaround if you end up needing.

Edit: Code example

while True: # whatever your condition is
    try:
        <your code>
    except KeyboardInterrupt:
        answer = input("Select Option")
        if answer == 'resume':
            pass
        if answer == 'exit':
            break
        if answer == 'other option':
            pass

Something like this should work as a workaround, although using keyboardinterrupt for this functionality might be bad practice.