0

I've been trying to make a stopwatch that my peers have challenged me to build, but I want it to be able to stop the timer when I type in a certain key into the python shell.

import time

timer = 0
timeTrueFalse = True

startTimer = input("Type start if you want to start!")

if (startTimer == "start"):
    while timeTrueFalse:
            timer = (timer + 1)
            print (timer)
            time.sleep(1)
Chris Martin
  • 30,334
  • 10
  • 78
  • 137
Adam
  • 5
  • 3
  • 2
    *Any* key or a *specific* key? Pressing Control+C raises a `KeyboardInterrupt`, which you can catch. Also, you don't need the parenthesis in the `if` statement. `if startTimer == "start":` will work fine ;-) – Martin Tournoij Mar 08 '16 at 02:44
  • @Carpetsmoker: Good thinking with Ctrl+C. I didn't think of that. – zondo Mar 08 '16 at 02:46
  • 2
    Read this. There are a couple of links in there as well that might help you. http://stackoverflow.com/questions/14400806/stopwatch-in-python-count-until-you-press-space – idjaw Mar 08 '16 at 02:46

1 Answers1

0

You can use timestamps

import time

while not str(raw_input("write start: ")) == "start" :
    pass

tstamp=time.time()

while not str(raw_input("write stop: ")) == "stop" :
    pass    

delta=time.time()-tstamp

#delta has the time in seconds.
xvan
  • 4,554
  • 1
  • 22
  • 37
  • "but I want it to be able to stop the timer when I type in a certain key into the python shell" – Jared Goguen Mar 08 '16 at 05:48
  • So you put the exit validation before the delta line. You don't need sleep if you can use the system timer – xvan Mar 08 '16 at 05:58