使用python shell结束whileloop

时间:2016-03-08 02:41:14

标签: python

我一直试图制作一个秒表,我的同行已经挑战我构建,但我希望它能够在我输入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)

1 个答案:

答案 0 :(得分:0)

您可以使用时间戳

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.