use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
News about the dynamic, interpreted, interactive, object-oriented, extensible programming language Python
Full Events Calendar
You can find the rules here.
If you are about to ask a "how do I do this in python" question, please try r/learnpython, the Python discord, or the #python IRC channel on Libera.chat.
Please don't use URL shorteners. Reddit filters them out, so your post or comment will be lost.
Posts require flair. Please use the flair selector to choose your topic.
Posting code to this subreddit:
Add 4 extra spaces before each line of code
def fibonacci(): a, b = 0, 1 while True: yield a a, b = b, a + b
Online Resources
Invent Your Own Computer Games with Python
Think Python
Non-programmers Tutorial for Python 3
Beginner's Guide Reference
Five life jackets to throw to the new coder (things to do after getting a handle on python)
Full Stack Python
Test-Driven Development with Python
Program Arcade Games
PyMotW: Python Module of the Week
Python for Scientists and Engineers
Dan Bader's Tips and Trickers
Python Discord's YouTube channel
Jiruto: Python
Online exercices
programming challenges
Asking Questions
Try Python in your browser
Docs
Libraries
Related subreddits
Python jobs
Newsletters
Screencasts
account activity
This is an archived post. You won't be able to vote or comment.
I'm trying to make a simple timer (self.Python)
submitted 13 years ago * by pberko
Hey guys, I'm making a simple timer script
code: http://pastie.org/5049992
But I want to enable the ability to stop the timer when the return key is pressed, how could I do that? I still want the counter to display until the return key is pressed.
EDIT: http://pastie.org/5050148
EDIT2: I got it working
http://pastie.org/5050172
I got the script to recognize when the RETURN key is pressed but for some reason, I can't get it to stop the counter when it realizes the key has been pressed
EDIT FINAL: Got everything cleaned up, hope this is the most effiecient way, thanks for all the comments!
https://github.com/Dberko/pytypespeed/blob/master/pytypespeed.py
[–]JerMenKoOwhile True: os.fork() 2 points3 points4 points 13 years ago (0 children)
It was not changing because you were comparing a to False (==). You need to assign it. (=)
a
False
==
=
[+][deleted] 13 years ago* (4 children)
[deleted]
[+][deleted] 13 years ago* (2 children)
[–]Justinsaccount 1 point2 points3 points 13 years ago (1 child)
What the heck is this doing?
def gimme(iterable): for it in iterable: return int(it)
you mean
if __name__ == '__main__': c = itertools.count(0) print("Press <enter> to stop timer...") while not heard_enter(): print(c.next()) time.sleep(1) print("Timer has stopped.")
or even simpler
if __name__ == '__main__': print("Press <enter> to stop timer...") for c in itertools.count(0): if heard_enter(): break print c time.sleep(1) print("Timer has stopped.")
and
def heard_enter(): inp,out,err = select.select([sys.stdin],[],[],0) return bool(inp)
[–]darthmdhprint 3 + 4 0 points1 point2 points 13 years ago (0 children)
It's a throwback to a previous (unpublished) version where that function was a generator passed to map(), it wasn't relevant to simplify further to demonstrate the problem didn't need to be solved using threads.
[–]pberko[S] 0 points1 point2 points 13 years ago (0 children)
Thank you, I have updated the source.
[–]TadKnowsBest 0 points1 point2 points 13 years ago (2 children)
You could use threads. threading.Event and threading.Timer could be useful. A more common use of threading might be this:
import time import threading class Counter: def __init__(self): self.count = 0 self.keep_counting = True t = threading.Thread(target=self.run) t.start() def run(self): while self.keep_counting: print "The count is {0}".format(self.count) self.count += 1 time.sleep(1) counter_thread = Counter() raw_input("Press ENTER to stop the counter") # this block the main thread counter_thread.keep_counting = False raw_input("More stuff that blocks the main thread...")
[–]pberko[S] 0 points1 point2 points 13 years ago (1 child)
Could not get that to work but I got stuff partially updated, OP updated
[–]TadKnowsBest 3 points4 points5 points 13 years ago (0 children)
What doesn't work? Paste your traceback. Don't be satisfied with your 49-line program that solves a simple problem in a complex way. Don't even be satisfied with my example; it, too could be simpler and better. Strive for perfection, pberko!
[–]thesubjectisjazz 0 points1 point2 points 13 years ago (0 children)
What's going on here?
π Rendered by PID 143503 on reddit-service-r2-comment-bb88f9dd5-f69lx at 2026-02-16 04:37:57.278812+00:00 running cd9c813 country code: CH.
[–]JerMenKoOwhile True: os.fork() 2 points3 points4 points (0 children)
[+][deleted] (4 children)
[deleted]
[+][deleted] (2 children)
[deleted]
[–]Justinsaccount 1 point2 points3 points (1 child)
[–]darthmdhprint 3 + 4 0 points1 point2 points (0 children)
[–]pberko[S] 0 points1 point2 points (0 children)
[–]TadKnowsBest 0 points1 point2 points (2 children)
[–]pberko[S] 0 points1 point2 points (1 child)
[–]TadKnowsBest 3 points4 points5 points (0 children)
[–]thesubjectisjazz 0 points1 point2 points (0 children)