【发布时间】:2017-07-26 17:37:12
【问题描述】:
我在 python 中制作了一个超级基本的秒表,只是为了让自己更适应它,(这是我的第一种编程语言)并且我一直试图让代码在按下键时停止运行(使用键盘中断) 但代码基本上忽略了键盘中断并一直持续下去。 (这是我的代码供参考)
# Time
import time
import sys
timeLoop = True
# Variables to keep track and display
Sec = 0
Min = 0
Hour = 0
# Begin Process
timeloop = True
while timeloop == True:
try:
Sec += 1
print(str(Hour) + " Hrs " + str(Min) + " Mins " + str(Sec) + " Sec ")
time.sleep(1)
if Sec == 60:
Sec = 0
Min += 1
print(str(Min) + " Minute")
if Min == 60:
Sec = 0
Min = 0
Hour += 1
print(str(Hour) + " Hours")
except KeyboardInterrupt:
sys.exit(0)
【问题讨论】:
-
不一定回答问题,但是为什么要定义两次
timeloop,也不需要做while timeloop == True:,你可以做while timeloop: -
在这种情况下,由于从未使用过 timeloop 变量,它可能只是
while True:。 -
你使用的是 Python 2 还是 Python 3?