如何在 python 中与基于文本的应用程序中的其他功能交互时获取经过的时间
how to get the elapsed time while interacting with other functions in a text-based application in python
所以我创建了这个记录足球(足球)统计数据的项目。到目前为止,它只记录角球和任意球,但我想记录两队之间的控球时间。我的主要问题是,当我试图保持占有时间时,其他功能将不起作用。这是我尝试过的:
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()
这行不通,但这是我的想法。如果您不明白我的意思,请在下方评论,以便我更详细地解释。
鉴于(如您在评论中所说)代码在循环中被重复调用,您有两个问题。
elapsed_home
在 'away_team'
分支而不是 'home_team'
分支中设置。修复起来很简单,只需将其向上移动几行即可。
- 您的所有变量都在函数内部声明 - 这意味着它们不存在于函数外部,并且它们不会在调用之间持续存在。您有三个(合理的)选项来解决此问题:
- 全局声明它们(在函数的顶部,例如
global elapsed_home
.
- 每次都将它们作为参数传递。 Return 每次都是。
- 使用 class 来保存它们,使其成为 class 上的方法而不是函数。
任你选。
勾选这个
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()
所以我创建了这个记录足球(足球)统计数据的项目。到目前为止,它只记录角球和任意球,但我想记录两队之间的控球时间。我的主要问题是,当我试图保持占有时间时,其他功能将不起作用。这是我尝试过的:
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()
这行不通,但这是我的想法。如果您不明白我的意思,请在下方评论,以便我更详细地解释。
鉴于(如您在评论中所说)代码在循环中被重复调用,您有两个问题。
elapsed_home
在'away_team'
分支而不是'home_team'
分支中设置。修复起来很简单,只需将其向上移动几行即可。- 您的所有变量都在函数内部声明 - 这意味着它们不存在于函数外部,并且它们不会在调用之间持续存在。您有三个(合理的)选项来解决此问题:
- 全局声明它们(在函数的顶部,例如
global elapsed_home
. - 每次都将它们作为参数传递。 Return 每次都是。
- 使用 class 来保存它们,使其成为 class 上的方法而不是函数。
- 全局声明它们(在函数的顶部,例如
任你选。
勾选这个
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()