Countdown from 2 minutes - how?

Discussion in 'Python' started by henrik@wollesens.dk, Mar 30, 2006.

  1. Guest

    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
     
    , Mar 30, 2006
    #1
    1. Advertising

  2. 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>
     
    Fredrik Lundh, Mar 30, 2006
    #2
    1. Advertising

  3. Fuzzyman Guest

    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
     
    Fuzzyman, Mar 30, 2006
    #3
  4. Guest

    #!/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()
     
    , Mar 30, 2006
    #4
  5. Guest

    #!/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()
     
    , Mar 30, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ryan Stewart

    GUI countdown timer in a JApplet?

    Ryan Stewart, Dec 23, 2003, in forum: Java
    Replies:
    2
    Views:
    871
  2. vasilis

    Countdown

    vasilis, Aug 23, 2004, in forum: Java
    Replies:
    2
    Views:
    696
    Nick Pomfret
    Aug 23, 2004
  3. Replies:
    5
    Views:
    580
    Andrew Thompson
    Nov 30, 2004
  4. JSP countdown timer

    , Nov 1, 2005, in forum: Java
    Replies:
    4
    Views:
    14,778
  5. Simon Brunning

    Re: Python Countdown

    Simon Brunning, Nov 11, 2005, in forum: Python
    Replies:
    0
    Views:
    470
    Simon Brunning
    Nov 11, 2005
Loading...

Share This Page