Hey there!

I'm currently trying to get my first text-adventure-game in python started,and I wanted to import some features that are totally unnecessary. just for the lulz.

So,I want to start a timer,that starts when the game starts,and ends,when the player is dead or won the game.

I want it to be like

print "You won the game, %s ! You escaped in X seconds/minutes!"

OR for the death function

print "You lost the game, %s. You died after X seconds/minutes!"

NOTE: I DO NOT NEED THE FUNCTION FOR THE DEATH OR SUCH, I JUST NEED THE TIMER FUNCTION

You can use datetime

>>> import datetime as dt
>>> 
>>> start = dt.datetime.now()
>>> start
datetime.datetime(2013, 7, 16, 0, 21, 17, 748591)
>>> # drink some cofee
... 
>>> death_date = dt.datetime.now()
>>> elapsed = death_date - start
>>> elapsed
datetime.timedelta(0, 81, 872052)
>>> elapsed.total_seconds()
81.872052

You can use datetime

import datetime as dt

start = dt.datetime.now()
start
datetime.datetime(2013, 7, 16, 0, 21, 17, 748591)

drink some cofee

...
death_date = dt.datetime.now()
elapsed = death_date - start
elapsed
datetime.timedelta(0, 81, 872052)
elapsed.total_seconds()
81.872052

I want that in my python script. How do I add it into? My try has failed.

import datetime as dt

start = dt.datetime.now()
start
print "Sup,what you gonna do?"
next = raw_input("> ")

if "this" in next:
    print "ok"
    death_datetime = dt.datetime.now()
    elapsed = death_date - start
    elapsed
    elapsed.total_seconds()
    exit(0)

else:
    print "ok"
    death_datetime = dt.datetime.now()
    elapsed = death_date - start
    elapsed
    elapsed.total_seconds()
    exit(0)

Try print 'elapsed: %.2f seconds' % elapsed.total_seconds()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.