![]() |
|
#1
|
|||
|
|||
Simple countdown from 2 minutesHi.
Can anyone tell me the python code for a simple countdown from eg. 2.00 minutes. It should be printet out to the screen. When it is finished it should write "Time is up" Hope you can help. Henrik |
|||
|
#2
|
||||
|
||||
Re: Simple countdown from 2 minutesDo you have something already written so we can see where the problem is or are you just curious?
__________________
Start Programming with Python-A beginner's guide to programming and the Python language. ------------- Common Sense v2.0-Striving to make the world a little bit smarter. |
|
#3
|
|||
|
|||
Re: Simple countdown from 2 minutesQuote:
Hi. In the meanwhile i have recieved this code: -------------------- import time def countdown(secs, interval=1): while secs > 0: yield secs secs = secs - 1 time.sleep(interval) print 'Tæller ned fra 120 sekunder:' for count in countdown(120): print count --------------------- AND this code: -------------------- import time start = time.time() lastprinted = 0 finish = start + 120 while time.time() < finish: now = int(time.time ()) if now != lastprinted: print int(finish - now) lastprinted = now time.sleep(0.5) # this stops the system hanging whilst this is running print "Time is up" ------------------- They do exactly the same ... Is it possible that it writes: 1.59 1.58 1.57 ... 1.39 .... 3 2 1 0 TIME IS UP And about the sleep function - i have some other stuff running - but the sleep functions of course stop all the other functions - how can i avoid that? Thanks for now ;-) |
|
#4
|
||||
|
||||
Re: Simple countdown from 2 minutesHere's some info on the time.sleep() function: http://www.faqts.com/knowledge_base/view.phtml/aid/2609. Not sure if it will answer your question though. The actual Python doc for time() is http://python.org/doc/current/lib/module-time.html.
__________________
Start Programming with Python-A beginner's guide to programming and the Python language. ------------- Common Sense v2.0-Striving to make the world a little bit smarter. |
|
#5
|
|||
|
|||
Re: Simple countdown from 2 minutesQuote:
Disclaimer: I am not a "python guy", and what I am about to post may not be the "best" way to do it in python. I wouldn't be shocked to find that there are people who could show more elegant, python-esque, ways to do this, but the following works for me. I am one of those funny guys who generally approach number problems from a mathematical/arithmetical point of view. Given a variable t that represents time in seconds, as in one of your countdown scripts, I use integer arithmetic to separate out the number of minutes and seconds. Then I print the number of seconds as a 2-digit decimal number where 1 sec is printed .01, 2 sec is printed .02, etc I show two ways to calculate the number of remainder seconds (I commented out one of the ways --- if you are interested, you can try that one and see if the results make sense). Some people like one way, some like the other. Python Code:
Try it, and let me know if it works for you. If anyone has a better way, I'd like to learn a little python myself. Regards, Dave Footnote: If you want "US-style" printouts where minutes and seconds are denoted mm:ss instead of mm.ss, you could try the following in place of the first print statement above: Python Code:
[edit] As I look at this, I realize that the first print statement above could have been: Python Code:
The point being that, once you have separated the minutes and remainder seconds, you can combine them in an output format any way you want. [/edit] No extra charge. Last edited by davekw7x : 15-Apr-2006 at 14:32.
|
Recent GIDBlog
Install Adobe Flash - Without Administrator Rights by LocalTech
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| UpdateData - simple MFC project | kobycool | MS Visual C++ / MFC Forum | 3 | 23-Oct-2005 02:45 |
| Simple Calculator Application | MOHAMMEDALI1989 | Java Forum | 1 | 06-Oct-2005 17:00 |
| Just need one simple example or link to such example | Doggonit | MySQL / PHP Forum | 2 | 31-Jul-2005 18:39 |
| Program that displays the minutes and hours | tommy69 | C Programming Language | 9 | 08-Mar-2004 16:57 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The