0

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.

Vivian
  • 1,539
  • 14
  • 38
mangafas
  • 43
  • 7
  • The code you've got there lets the user pick one of `'fk'`, `'ck'`, `'home team'`, `'away_team'`, or `'q'` each time it runs. It doesn't loop, so nothing's going to be kept between runs. Is that what you intended? There's other problems, but the fixes depend on that answer. – Vivian Sep 06 '16 at 20:10
  • no i want it to loop, my program is bigger with more stats but this is just a preview – mangafas Sep 06 '16 at 20:12

2 Answers2

1

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.

Vivian
  • 1,539
  • 14
  • 38
  • well yes that worked but it didn't answer my main question: while its recording time i can not record ck nor fk. I would have to hit enter and then. What i want is when i type the away team, the clock for the home team ends and the clock for the away starts but if for example while home has the ball and gets a free kick i want it to count time without stopping it and then record the stat – mangafas Sep 06 '16 at 22:50
1

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()
finmor
  • 451
  • 4
  • 7
  • i didn't understand everything that you just said but i will get back to you when i research a little bit more on threading – mangafas Sep 07 '16 at 00:07
  • this works but i think it has a mistake while counting because when i format it so it prints the percentage it always shows that away is 100% but home is 0% `total_num = elapsed_home + elapsed_away perc_num = 100 / total_num final_home = elapsed_home * perc_num final_away = elapsed_away * perc_num print(final_home) print(final_away) round_home = round(final_home, 1) round_away = round(final_away, 1) string_home = str(round_home) string_away = str(round_away) print_home = string_home + "%" print_away = string_away + "%" print(print_home) print(print_away) ` – mangafas Sep 07 '16 at 16:03
  • for some reason it doesn't format it as code with backticks – mangafas Sep 07 '16 at 16:07
  • also where do i put ` if ball_away: start_away = time.time() else: start_home = time.time() ` – mangafas Sep 07 '16 at 16:10
  • This if is not a part of the body() function. You should place it just before calling body() – finmor Sep 07 '16 at 17:09
  • you are a god @finmor it worked... now what is left is to sell your code...Kappa – mangafas Sep 07 '16 at 18:01
  • I'm glad I've helped. Good luck with your project! – finmor Sep 07 '16 at 18:10