Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Consider using <a href="http://docs.python.org/2/library/threading.html#thread-objects" rel="nofollow"><code>threading.Thread</code></a>:</p> <pre><code>import time import threading class MyTimer(threading.Thread): def __init__(self): self.h = 0 self.m = 0 self.s = 0 def count(self, t, stop_event): while self.s &lt;= 60: print self.h, 'Hours', self.m, 'Minutes', self.s, 'Seconds' time.sleep(1) self.s += 1 if self.s == 60: self.m += 1 self.s = 0 elif self.m == 60: self.h += 1 self.m = 0 self.s = 0 elif stop_event.is_set(): print self.h, 'Hours', self.m, 'Minutes', self.s, 'Seconds' break class Asking(threading.Thread): def asking(self, t, stop_event): while not stop_event.is_set(): word = raw_input('enter a word:\n') if word == 'bye': timer_stop.set() question_stop.set() timer = MyTimer() question = Asking() question_stop = threading.Event() timer_stop = threading.Event() threading.Thread(target=question.asking, args=(1, question_stop)).start() threading.Thread(target=timer.count, args=(2, timer_stop)).start() </code></pre> <p>Running it as an example:</p> <pre><code>$ python stackoverflow.py enter a word: 0 Hours 0 Minutes 0 Seconds 0 Hours 0 Minutes 1 Seconds 0 Hours 0 Minutes 2 Seconds 0 Hours 0 Minutes 3 Seconds hi enter a word: 0 Hours 0 Minutes 4 Seconds 0 Hours 0 Minutes 5 Seconds 0 Hours 0 Minutes 6 Seconds bye 0 Hours 0 Minutes 7 Seconds </code></pre> <p>The code could probably be a bit more neater :p. I shocked myself that I was able to produce this :D.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload