How Do I Loop An Input In Python?

389 Views Asked by At

I have been searching and searching on how to figure out how to make an input or something go into a while loop. As in, the input() command won't stop my stopwatch. I have tried tkinter, pygame, and a couple other methods, but they just didn't work. If anyone can help me out, I would prefer something small and simple, if that's even possible. And to be specific on what I want to learn to do, is basically allowing, when any key is pressed, for it to instantly stop (preferably without hitting enter). Thanks, saddlepiggy!

Here is what I have so far, with nothing to activate the stopping:

    #Setup (Variables and stuff)
        hours = 0
        minutes = 0
        seconds = 0
        import time



    #Main Part of Code
    print("Welcome to PyWatch, a stopwatch coded in Python!")
    print("Press any key to start the stopwatch.")
    print("Then, press any key to stop it!")
    start = input("")

    while hours < 48:
        seconds = seconds + 1
        time.sleep(1)
        print(hours, "hours,", minutes, "minutes,", seconds, "seconds")



    #If Statements for getting seconds/minutes/hours
    if (seconds == 60):
        minutes = minutes + 1
        seconds = seconds - 60

    if (minutes == 60):
        hours =hours + 1
        minutes = minutes - 60
2

There are 2 best solutions below

2
On

Threading is what you want.

Create a second thread that waits for the input while your first thread handles your stopwatch code. Behold:

 import threading, sys

 def halt():
     raw_input()

 threading.Thread(target=halt).start()


 while hours < 48 and threading.active_count() > 1:
     seconds = seconds + 1
     time.sleep(1)

     # copy and past what you had before

Allow me to elaborate on what's happening: Thus far, all of the code you've written has been single threaded. This means only one line of code is executed at a time, there's only one thread of execution. Consequentially, your script can't multitask, it can't wait for input and print the time at the same time.

So when this line is evaluated

threading.Thread(target=halt).start()

The main thread creates a second thread of execution. Meanwhile, the main thread goes on and enters the while loop. The target parameter is the entry point of the thread, it's the starting point. It's analogous to if __name__ == "__main__:" for the main thread. Just as the main thread terminates when it reaches the end of if __name__ == "__main__:", our second thread will terminate once it reaches the end of halt().

The threading.active_count function tells you how many threads in are current in execution.

0
On

You can't do that in Python. You are asking for a keyboard event. Keyboard events are shipped with a GUI. This thread explains pretty much everything: QKeyPress event in PyQt

Or use an external application for your OS that can append output to a file that is read by your Python program in a loop. When a certain event is detected you can perform some actions. For Linux this tread explains: https://superuser.com/questions/248517/show-keys-pressed-in-linux