GIDForums  

Go Back   GIDForums > Computer Programming Forums > Python Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 29-Mar-2006, 07:37
miniwolle miniwolle is offline
New Member
 
Join Date: Mar 2006
Posts: 2
miniwolle is on a distinguished road

Simple countdown from 2 minutes


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
  #2  
Old 30-Mar-2006, 19:16
crystalattice's Avatar
crystalattice crystalattice is offline
Aspiring author
 
Join Date: Apr 2004
Location: Japan (again)
Posts: 1,635
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice

Re: Simple countdown from 2 minutes


Do 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  
Old 31-Mar-2006, 06:12
miniwolle miniwolle is offline
New Member
 
Join Date: Mar 2006
Posts: 2
miniwolle is on a distinguished road

Re: Simple countdown from 2 minutes


Quote:
Originally Posted by crystalattice
Do you have something already written so we can see where the problem is or are you just curious?

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  
Old 31-Mar-2006, 22:08
crystalattice's Avatar
crystalattice crystalattice is offline
Aspiring author
 
Join Date: Apr 2004
Location: Japan (again)
Posts: 1,635
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice

Re: Simple countdown from 2 minutes


Here'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  
Old 15-Apr-2006, 13:28
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,309
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Simple countdown from 2 minutes


Quote:
Originally Posted by miniwolle

Is it possible that it writes:
1.59
1.58
1.57
...
1.39
....
3
2
1
0

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:
# t is the time remaining (in seconds)
# Calculate "mins" = the number of whole minutes remaining 
# If time remaining is one minute or more:
#   Calculate "rsecs" = the number of seconds left after
#                       the time in "mins" has been taken away
#   So the time remaining is "mins" minutes + "rsecs" seconds
#   Will print rsecs as a decimal: 1 sec => .01, 2 sec => .02, etc.
#
# If the time remaining is less than a minute:
#   Just print the seconds
#

    if t >= 60:
      mins = int(t/60)
      #rsecs = t - mins * 60 #could use this
      rsecs = t % 60     #same results as t - mins * 60
      print "%.2f" % (mins + rsecs / 100.0)
    else:
      print t




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:
      print "%d:%02d" % (mins, rsecs)

[edit]
As I look at this, I realize that the first print statement above could have been:
Python Code:
      print "%d.%02d" % (mins, rsecs)

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 GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 01:37.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.