Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 19th, 2012, 12:33 PM
toddszee toddszee is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 2 toddszee User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 m 23 sec
Reputation Power: 0
Python Timer

I need a timer to start when my code is executed and to end when my code has run when a game has completed. To print the time taken in minutes and secs. To print a message at the end. Any ideas?

Reply With Quote
  #2  
Old November 19th, 2012, 02:21 PM
rrashkin's Avatar
rrashkin rrashkin is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Location: 39N 104.28W
Posts: 90 rrashkin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 13 h 45 m 4 sec
Reputation Power: 2
import datetime
t1=datetime.datetime.now()
....your code...
t2=datetime.datetime.now()
#convert to seconds
elpsd=(t2-t1)[0]*86400+(t2-t1)[1]+(t2-t1)[2]/1000000.0
print "elapsed time: {0:12g} minutes".format(elpsd/60)

Reply With Quote
  #3  
Old November 19th, 2012, 03:45 PM
Dietrich's Avatar
Dietrich Dietrich is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 483 Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 3 Days 22 h 51 m 26 sec
Reputation Power: 63
One idea:
Code:
import time
import sys

# make this work with Python2 or Python3
if sys.version_info[0] < 3:
    input = raw_input


# seconds since epoch now at start
start = time.time()

# a silly game to test the counter
a = input("Enter your name: ")
b = input("Enter your age: ")
c = input("Enter your weight: ")
d = input("What year were you born? ")
e = input("What is capitol of France? ")

# the game is finished get seconds since epoch now
end = time.time()

elapsed = end - start

print("This game took %d minutes and %d seconds" % (divmod(elapsed, 60)))
__________________
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25

Reply With Quote
  #4  
Old November 19th, 2012, 04:09 PM
toddszee toddszee is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 2 toddszee User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 m 23 sec
Reputation Power: 0
Solutions

Thanks for the solutions, I have have got them both working. I am new to Python, it looks like I have a long journey ahead.

Reply With Quote
  #5  
Old November 20th, 2012, 02:02 PM
Yegers Yegers is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 26 Yegers User rank is Sergeant (500 - 2000 Reputation Level)Yegers User rank is Sergeant (500 - 2000 Reputation Level)Yegers User rank is Sergeant (500 - 2000 Reputation Level)Yegers User rank is Sergeant (500 - 2000 Reputation Level)Yegers User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 12 h 7 m 8 sec
Reputation Power: 0
sorry negate what I just wrote, I misread the question...

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Python Timer

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap