【问题标题】:KeyboardInterrupt not working as I've thought it would, gotta know why and how to fixKeyboardInterrupt 没有像我想象的那样工作,必须知道为什么以及如何修复
【发布时间】: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


【解决方案1】:

这是因为except 处理块在while True: 循环之外。

当您第一次按 ctrl-C 并选择继续时,start_chronometer 第二次递归运行。但是当递归函数退出时,原来的执行一直在继续,直到函数结束,因为它不再处于循环中。

重新排列您的代码以将 while True 循环放在所有内容中:

while True:
    try:
        n += 1
    except:
        ...

您还需要在用户选择停止时添加break 语句,以便循环终止。

【讨论】:

  • 我了解您的解决方案,我认为这也是一种可能性,但它仍然不起作用......同样的问题。程序在第二次中断时停止
  • 我不明白这怎么可能。你能用你的新代码更新问题吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-28
  • 1970-01-01
相关资源
最近更新 更多