Countdown from 2 minutes - how?

H

henrik

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
 
F

Fredrik Lundh

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

Fuzzyman

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
 
P

preved.rly

#!/bin/env python
# countdown.py
import time
import doctest

def countdown( time_in_minutes):
""" 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()
 
P

preved.rly

#!/bin/env python
# countdown.py

import time
import doctest

def countdown( time_in_minutes):
""" 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()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top