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 problem with your code is that when you run <code>hall()</code>, Python first executes the whole of <code>timer()</code> (i.e. the whole <code>for</code> loop), and then moves on with the rest of the code (it can only do one thing at a time). Thus, by the time it reaches the <code>while</code> loop in <code>hall()</code>, <code>timer</code> is already <code>0</code>.</p>

<p>So, you're going to have to do something about that <code>timer</code> so that it counts down once, and then it moves on to the <code>do something</code> part.</p>

<p>Something that you can do is this:</p>

<pre class="lang-python prettyprint-override"><code>def hall():
    for a in range(0, 15):
        print(15 - a)
        # do something
        time.sleep(1)
</code></pre>

<p>This should work just fine (if you're only executing <code>hall</code> 15 times), and condenses your code to just one function.</p>

python timer