Python: accept user input anytime

I am creating a block that will do several things, one of which is counting the cycles of the machine. While I hand this over to the ladder logic (CoDeSys), I put my ideas in Python first.

I will have a counter, just with

    counter += 1
    print("counter")

      

to keep track of which cycle I'm on. However, I want to reset this counter at any time, preferably by typing "RESET". I understand how to use the enter command,

    check = input()

      

however, I do not know how to run the program while searching for input, or is it possible at all. Thanks in advance for any answer.

If it helps to understand, here is the code. The problem is where is the problem. http://pastebin.com/TZDsa4U4

+3


source to share


1 answer


If you only want to signal to reset the counter, you can catch the KeyboardInterrupt exception.



while True:
    counter = 0
    try:
        while True:
            counter += 1
            print("counter")
    except KeyboardInterrupt:
        pass

      

+2


source







All Articles