how to get the elapsed time while interacting with other functions in a text-based application in python
<p>So I created this project that records football (soccer) stats. Up to now it records only corner and free kicks but I want to record the possession time between the 2 teams. My main problem is that when I try to keep possession time, the other functions won't work. Here's what I've tried:</p>

<pre><code>import time

fk_range = range(0, 1000)
fk_list = list(fk_range)
ck_range = range(0, 1000)
ck_list = list(ck_range)


def body():
    while True:
        choice = input("")
        if choice == "fk":

            first_number_fk = fk_list[0]
            fk_list.remove(first_number_fk)
            number_fk = fk_list[0]
            string_number_fk = str(number_fk)
            print("Free Kick(s)", string_number_fk)

        elif choice == "ck":

            first_number_ck = ck_list[0]
            ck_list.remove(first_number_ck)
            number_ck = ck_list[0]
            string_number_ck = str(number_ck)
            print("Corner Kick(s): ", string_number_ck)

        if choice == "home team":
            start_home = time.time()
            input()
            end_home = time.time()
        if choice == "away_team":
            start_away = time.time()
            input()
            end_away = time.time()
            elapsed_home = end_home - start_home
            elapsed_away = end_away - start_away

        elif choice == "q":
            print(elapsed_away)
            print(elapsed_home)

body()
</code></pre>

<p>This doesn't work, but this is an idea of what I've got. If you didn't understand what I mean, please comment below so I can explain in more detail.</p>

<p>Given that (as you said in comments) that code is called repeatedly in loops, you have two issues.</p>

<ul>
<li><code>elapsed_home</code> is being set in the <code>'away_team'</code> branch instead of the <code>'home_team'</code> branch. Simple enough to fix, just move that up a few lines.</li>
<li>All of your variables are declared inside the function - which means they don't exist outside of it, and they don't persist between calls. You have three (reasonable) options to fix this:

<ol>
<li>Declare them all globally (at the top of the function, put e.g. <code>global elapsed_home</code>.</li>
<li>Pass them all in as arguments every time. Return them all every time.</li>
<li>Use a class to hold them, make it a method on that class instead of a function.</li>
</ol></li>
</ul>

<p>Take your pick.</p>

python input output text-based