2016-09-29 157 views
0

我試圖發送一個End命令到這行代碼的末尾。 我搜索無處不在,沒能找到一個有價值的答案..Python發送命令

if args.time: 
    def stopwatch(seconds): 
     start = time.time() 
     time.clock()  
     elapsed = 0 
     while elapsed < seconds: 
      elapsed = time.time() - start 
      print "Sending for(900) Seconds: %f, seconds count: %02d" % (time.clock() , elapsed) 
      time.sleep(1) 

stopwatch(5) 

婉婷送一些一Control C排序是來阻止它,而不 退出程序。

回答

0

您可以使用control-c。

你只需要趕上KeyboardInterrupt例外。

try: 
    # your loop code here 
except KeyboardInterrupt: 
    # do whatever you want when user presses ctrl+c 
1

你想趕上一個KeyboardInterrupt

if args.time: 
    def stopwatch(seconds): 
     start = time.time() 
     time.clock()  
     elapsed = 0 
     while elapsed < seconds: 
      try: 
       elapsed = time.time() - start 
       print "Sending for(900) Seconds: %f, seconds count: %02d" % (time.clock() , elapsed) 
       time.sleep(1) 
      except KeyboardInterrupt: break 

    stopwatch(5)