什么是可以接受的在一个国家

2024-10-03 06:22:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个try语句,大致如下所示。你知道吗

for result in results['matches']:
    try:
        #runs some functions
    except KeyboardInterrupt:
        leaveopt = raw_input( 'Would you like to exit or skip the current match? [e/s]:' )
        if leaveopt == 'e':
            print '\nExiting...'
        else:
            print '\nSkipping match...'

当我运行程序时,我没有得到任何错误,但当我按下ctrl-c时,它只是跳过当前匹配,而不是询问我想做什么。我想知道是只有一些内容可以在try语句的except部分运行,还是还有其他问题。你知道吗


Tags: informatchrunssome语句resultfunctions
1条回答
网友
1楼 · 发布于 2024-10-03 06:22:40

据我所知,对在except中可以做什么没有限制(即使这里有一些您可能希望避免的事情),这对我很有用(python 2.7.3/linux mint):

import time

for x in xrange(5000):
    try:
        print x
        time.sleep(5)
    except KeyboardInterrupt, e:
        leaveopt = raw_input( 
            'Would you like to exit or skip the current match? [e/s]:'
            )
        if leaveopt == 'e':
            print '\nExiting...'
            break
        else:
            print '\nSkipping match...'
            continue

问题显然出在“运行一些函数”部分的某个地方。你知道吗

相关问题 更多 >