【发布时间】:2021-09-17 18:53:36
【问题描述】:
我已经使用 python 构建了一个计时器,一开始很简单,然后我添加了一个 resume pause/resume 命令。所以在第一段中,keyboardInterrupt 工作得很好,但是当我重新启动进程时,它会重新启动,进入循环,但是当我中断执行时,它只是完成程序而不继续过去的语句和转发。我想了解为什么会这样,请 下面的代码: 我在这里回答后做了一点改变,但也没有用。所以我在我的初始代码下面传递了修改后的代码。
'''蟒蛇
def start_chronometer(passed: float = 0):
import time
beginning = time.time()
n = 0
try:
while True:
n += 1
except KeyboardInterrupt:
past = (time.time() - beginning) + passed
possibilities = ['Y', 'N']
while (choice := input('Wish to resume counting ? y - Yes / n - No: ').title()) not in possibilities:
print('Valor precisa ser "s" ou "n"')
if choice == possibilities[0]:
start_chronometer(passed=past)
else:
if 60 <= past < 3600:
print(f'The time passed since the beginning of the counting was: {past / 60} minutes')
elif 3600 <= past:
print(f'The time passed since the beginning of the counting was: {past / 3600} hours')
else:
print(f'The time passed since the beginning of the counting was: {past} seconds')
start_chronometer()
def start_chronometer(passed: float = 0):
while True:
aux = passed
import time
beginning = time.time()
n = 0
try:
while True:
n += 1
except KeyboardInterrupt:
past = (time.time() - beginning) + aux
possibilities = ['Y', 'N']
while (choice := input('Wish to resume counting ? y - Yes / n - No: ').title()) not in possibilities:
print('Valor precisa ser "s" ou "n"')
if choice == possibilities[0]:
passed = past
pass
else:
break
if 60 <= past < 3600:
print(f'The time passed since the beginning of the counting was: {past / 60} minutes')
elif 3600 <= past:
print(f'The time passed since the beginning of the counting was: {past / 3600} hours')
else:
print(f'The time passed since the beginning of the counting was: {past} seconds')
start_chronometer()
'''
【问题讨论】:
-
无法重现问题,请更具体
-
当我重新启动进程时 你的意思是你真的再次运行程序吗?还是您的意思是您对问题提示回答“y”?
-
就是这样,我回答Y,然后它再次调用该函数,好吗?所以它“重新开始”并正常运行直到de while循环,当我导致第二个键盘中断时,程序就停止了
-
当我导致第二个键盘中断时,它应该再次询问我是否要恢复计数,但它没有,只是停止
标签: python loops exception keyboardinterrupt