Search code examples
pythoninputoutputtext-based

how to get the elapsed time while interacting with other functions in a text-based application in python


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:

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()

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.


Solution

  • Check this one

    elapsed_home = 0
    elapsed_away = 0
    start_home = 0
    start_away = 0
    ball_home = False
    ball_away = True # Assumimg that away team has the starting kick
    
    
    def body():
        global elapsed_home, elapsed_away, start_home, start_away, ball_home, ball_away
        while True:
           choice = input("")
           ...
           ...
           if choice == "home team":
               if ball_home:
                  continue:
               else:
                  ball_away = False
                  ball_home = True
                  start_home = time.time()
                  elapsed_away += time.time() - start_away
                  print "away elapsed: " + str(elapsed_away)
    
           if choice == "away_team":
                if ball_away:
                    continue
                else:
                    ball_away = True
                    ball_home = False
                    start_away = time.time()
                    elapsed_home += time.time() - start_home
                    print "home elapsed: " + str(elapsed_home)
           ...
     # Initialize depending on who has the starting kick
     if ball_away:
         start_away = time.time()
     else:
         home_start = time.time()
     body()