0

I am trying to make a counter so if I type 'bye' it starts counting how long it has been since I said bye but the problem is that I can't type anything to stop the counter and I don't know how to have it tell you something when you type something to stop it. Here is my code for a counter but I tried to type something and it does not stop:

import time
s = 0
m = 0
h = 0
while s<=60:
    print h, 'Hours', m, 'Minutes', s, 'Seconds'
    time.sleep(1)
    s+=1
    if s == 60:
        m+=1
        s = 0
    elif m ==60:
        h+=1
        m = 0
        s = 0
3
  • 1
    Can you be more specific as to exactly what you want? Do you want something where if you type q, for instance, your counter will stop (possibly do something else then)? Or is it something more complicated than that?
    – Jared
    Commented Jun 29, 2013 at 1:52
  • Also, what version of python are you using? Collecting user input changed in the crossover from 2.x -> 3.x
    – Brian
    Commented Jun 29, 2013 at 2:32
  • python2.7 is the one im using Commented Jun 29, 2013 at 2:40

3 Answers 3

1

Consider using threading.Thread:

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 <= 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()

Running it as an example:

$ 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

The code could probably be a bit more neater :p. I shocked myself that I was able to produce this :D.

0

The only way I know is with pygame. It would set up a normal pygame loop, except having it only being 1 by 1, so you can see the background window, and when you enter in a letter it would exit the pygame loop.

0

maybe better...

    .....
    .....
    while self.s <= 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

            if self.m == 60:
               self.h += 1
               self.m = 0

        elif stop_event.is_set():
            print self.h, 'Hours', self.m, 'Minutes', self.s, 'Seconds'
            break
        ......
        ......

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.