Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Countdown from 2 minutes - how?

Reply
Thread Tools

Countdown from 2 minutes - how?

 
 
henrik@wollesens.dk
Guest
Posts: n/a
 
      03-30-2006
Hi.

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

 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      03-30-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> 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"


here's a first version:

import time
time.sleep(2*60)
print "Time is up"

to improve this, I suggest reading the chapter on loops in the tutorial:

http://docs.python.org/tut/node6.html

</F>



 
Reply With Quote
 
 
 
 
Fuzzyman
Guest
Posts: n/a
 
      03-30-2006

(E-Mail Removed) wrote:
> Hi.
>
> 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.
>


You need the module ``time`` :

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"

HTH

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml

> Henrik


 
Reply With Quote
 
preved.rly@gmail.com
Guest
Posts: n/a
 
      03-30-2006
#!/bin/env python
# countdown.py
import time
import doctest

def countdown( time_in_minutes):
"""
>>> import countdown
>>> countdown.countdown(0.1) # doctest:+ELLIPSIS

6
5
...
1
Time is up
"""
for i in reversed(xrange(1, int(time_in_minutes * 60) + 1)):
print i
time.sleep(1)
print 'Time is up'

if __name__=='__main__':
doctest.testmod()

 
Reply With Quote
 
preved.rly@gmail.com
Guest
Posts: n/a
 
      03-30-2006
#!/bin/env python
# countdown.py

import time
import doctest

def countdown( time_in_minutes):
"""
>>> import countdown
>>> countdown.countdown(0.1) # doctest:+ELLIPSIS

6
5
...
1
Time is up
"""
for i in xrange(int(time_in_minutes * 60 + 0.5), 0, -1):
print i
time.sleep(1)
print 'Time is up'

if __name__=='__main__':
doctest.testmod()

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
JSP countdown timer agold791@hotmail.com Java 4 11-03-2005 02:17 AM
Countdown thread resume problem bojanraic@gmail.com Java 5 11-30-2004 05:35 PM
Countdown vasilis Java 2 08-23-2004 04:50 PM
GUI countdown timer in a JApplet? Ryan Stewart Java 2 12-24-2003 07:22 PM
Ram countdown "Is it realy required " Ockerr Computer Support 5 08-20-2003 01:11 PM



Advertisments