python 2.7 â getting this timer object right
<p>I'm making a text-based game to practice python.<br><br></p>

<p>The following is the code of the Trap scene. In the scene, the player falls into a hole, and if he doesn't react in time (5 seconds), he dies. After starting the timer, I ask for input inside a while loop. I'm seeing, that this code somehow redundant â as i define what happens after the time specified passes twice â but i'm not really sure how to fix it.<br><br></p>

<pre><code>def b_trap():
    """Trap. I want to implement a timing function here. The player will need to type the appropriate command under a given time. If he cannot make a decision in time, the player dies. Timer object!!!"""
    def b_timeout():
        """If player runs out of time, this is called."""
        b_dead("You fall into the deep hole in the floor, and die when you\nhit the floor fifty meters down. : (")

    print "The floor suddenly disappears behind your feet. You start to fall. What do you do? HURRY!"

    time_to_do_stuff = Timer(5.0, b_timeout)
    time_to_do_stuff.start()

    while time_to_do_stuff.is_alive() is True:
        command = raw_input(" &gt; ")

        if command in (bv_commands['climb'] or bv_commands['jump']):
            time_to_do_stuff.cancel()
            b_not_ready()
        else:
            print "You were saying?"
    else:
        b_timeout()
</code></pre>

<p>After running this code and not doing anything, <code>b_timeout()</code> is executed, but the <code>raw_input()</code> prompt remains â i'm asked for input again, but now without the prompt specified in the <code>raw_input</code> function(<code>" &gt; "</code>).<br><br></p>

<p>Could you point out for me what am I missing? I've just started learning python, so please forgive my novice mistakes.</p>

<p><code>raw_input</code> is a blocking function that effectively halts the program until the input is received (waits for a new line/carriage return).</p>

<p>There are two ways around this:</p>

<ul>
<li>Record the time before and after the <code>raw_input</code>. If the latter is less than five seconds after the former then they responded quickly enough. This approach will have a potentially unwanted behaviour of not showing player death until they respond.</li>
<li>Run the <code>raw_input</code> concurrently in a separate python thread <a href="http://stackoverflow.com/questions/30929661/non-blocking-raw-input-in-python">like this</a>.</li>
</ul>

python python-2.7 time