mangafas
mangafas

Reputation: 43

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.

Upvotes: 0

Views: 85

Answers (2)

finmor
finmor

Reputation: 451

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

Upvotes: 1

Vivian
Vivian

Reputation: 1639

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

  • elapsed_home is being set in the 'away_team' branch instead of the 'home_team' branch. Simple enough to fix, just move that up a few lines.
  • 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:
    1. Declare them all globally (at the top of the function, put e.g. global elapsed_home.
    2. Pass them all in as arguments every time. Return them all every time.
    3. Use a class to hold them, make it a method on that class instead of a function.

Take your pick.

Upvotes: 1

Related Questions