1

I'm trying to make a text based flight simulator in python, but I can't really find out how I can count seconds in python.

I would like to write code that lets me write, while the time counts in the background. I also want a simple code like for example Import Time then do time.sleep(1).

I tried to do this:

for takeoff_time in range(0,16):
    time.sleep(1)

Then:

while takeoff_time == (15):
break()
print ("Fly Crashed Before TakeOff ")
time.sleep(2)
os.system("cls")
main()
Hannele
  • 9,301
  • 6
  • 48
  • 68
Steffyz
  • 21
  • 2

2 Answers2

2

using the time module you can do:

start_time = time.time()

RUN THE PART OF THE PROGRAM YOU WANT TO MEASURE

print(time.time()-start_time)

or instead of printing it you can assign it to a variable to keep track of it

Samuel Barnett
  • 434
  • 1
  • 3
  • 13
1

In Python, you have time.clock() and time.time()

time.clock

start = time.clock()
... do something
elapsed = (time.clock() - start)

time.time

start = time.time()
... do something
elapsed = (time.time() - start)

For benchmarking code snippets, you should try using timeit: http://docs.python.org/2/library/timeit.html

Ali Gajani
  • 14,762
  • 12
  • 59
  • 100
  • Almost... It's Just one problem. How can i use it in my flight simulator? I mean, How can i count secounds as an integer, ill like to write at the same time as the secounds is counting. – Steffyz Aug 12 '13 at 16:33