Python : Basic countdown timer & function() > int()
<p>I'm trying to ucreate a timer function that runs in the background of my code and make it so I can use/check the time. What I mean by use/check, I'm trying to make it so I can call upon that timer function and use it as integer.</p>

<p>This is the code I currently have:</p>

<pre><code>def timer():
    for endtime in range(0, 15):
        print(15 - endtime)
        time.sleep(1)

def hall():
    timer()
    while (timer &gt; 0):
       do something
</code></pre>

<p>Currently only using <code>print(15 - endtime)</code> for confirmation it is counting down.</p>

<p>But what the code does now is execute the countdown and that's it, it never touches the while loop. And of course the last issue is I can't set a function to an int. So I'm looking for some way where I can check where the timer is at and use it in that while loop.</p>

<p>The way you do it, you'll going to have to use multithread.</p>

<p>Here is another, simpler approach :
On your script beginning, set a <code>time_start</code> variable with the number of seconds since the epoch using <code>time.time()</code>
Then when you need the number of elapsed seconds, use <code>time.time() - time_start</code> :</p>

<pre><code>t_start = time.time()
# do whatever you'd like
t_current = int(time.time()-t_start) # this way you get the number of seconds elapsed since start.
</code></pre>

<p>You can put that in a function as well, defining t_start as a global variable.</p>

<pre><code>import time
t_start = time.time()

def timer():
    global t_start
    print(str(int(time.time()-t_start)))


print('start')
time.sleep(2)
timer()
time.sleep(3)
timer()
</code></pre>

python timer