我是编码的新手,只是为了练习,我正在尝试为我的大学中的板球联赛创建联赛表。该表将随着联赛的进展而更新。但是我也想保留一个选项来检查具有各种列的表排名,但是每次我尝试检查排名时都会返回空表。
#function that calculates various parameters
def net_rr(n,i):
result = input('Won(w)/Lost(l)/Draw(d):')
if (result == 'w') or (result == 'l') or (result == 'd'):
if result == 'w':
Table[i][2] += 1
Table[i][5] += (3*Table[i][2])
elif result == 'l':
Table[i][3] += 1
Table[i][5] += 0*Table[i][3]
elif result == 'd':
Table[i][4] += 1
Table[i][5] += 1*Table[i][4]
Table[i][0] = n
Table[i][1] += 1
bat_score = float(input('batting score:'))
ovrs_played = float(input('over batted:'))
opp_score = float(input('opponent score:'))
opp_over = float(input('opponent over:'))
runs_scored = [0,0,0,0,0,0]
over_batted = [0,0,0,0,0,0]
runs_conceded = [0,0,0,0,0,0]
overs_bowled = [0,0,0,0,0,0]
runs_scored[i] += bat_score
over_batted[i] += ovrs_played
runs_conceded [i] += opp_score
overs_bowled [i] += opp_over
For = runs_scored[i] / over_batted[i]
Against = runs_conceded [i] / overs_bowled [i]
NRR = For - Against
Table[i][6] = NRR
for row in Table:
print(row)
print('\n')
#main code which asks user input whether he wants to update('Entry') or just check the standings ('Check')
def table( ):
Table = [['Name','Played','Won','Lost','Draws','Points','Net_RR'],['Team1',0,0,0,0,0,0],['Team2',0,0,0,0,0,0],['Team3',0,0,0,0,0,0],['Team4',0,0,0,0,0,0],['Team5',0,0,0,0,0,0],['Team6',0,0,0,0,0,0]]
q = input('what do you want to do:')
if q == 'Entry':
n = input('Name:')
if n == 'Team1':
i = 1
elif n == 'Team2':
i = 2
elif n == 'Team3':
i = 3
elif n == 'Team4':
i = 4
elif n == 'Team5':
i = 5
elif n == 'Team6':
i = 6
net_rr(n,i)
elif q == 'Check':
for row in Table:
print(row)
print('\n')
我希望每次检查站位时都会显示更新的表格,但是它始终显示一个空表:
运行
table()
给出以下输出:what do you want to do:Check
['Name', 'Played', 'Won', 'Lost', 'Draws', 'Points', 'Net_RR']
['Team1', 0, 0, 0, 0, 0, 0]
['Team2', 0, 0, 0, 0, 0, 0]
['Team3', 0, 0, 0, 0, 0, 0]
['Team4', 0, 0, 0, 0, 0, 0]
['Team5', 0, 0, 0, 0, 0, 0]
['Team6', 0, 0, 0, 0, 0, 0]
最佳答案
由于您是在函数体内定义“表格”变量的,因此无法全局查看。
将其移到您的功能之外,就可以了。
注意:我注释掉了几行只是为了测试您的代码
def net_rr(n,i):
result = input('Won(w)/Lost(l)/Draw(d):')
if (result == 'w') or (result == 'l') or (result == 'd'):
if result == 'w':
Table[i][2] += 1
Table[i][5] += (3*Table[i][2])
elif result == 'l':
Table[i][3] += 1
Table[i][5] += 0*Table[i][3]
elif result == 'd':
Table[i][4] += 1
Table[i][5] += 1*Table[i][4]
Table[i][0] = n
Table[i][1] += 1
bat_score = float(input('batting score:'))
ovrs_played = float(input('over batted:'))
opp_score = float(input('opponent score:'))
opp_over = float(input('opponent over:'))
# runs_scored = [0,0,0,0,0,0]
# over_batted = [0,0,0,0,0,0]
# runs_conceded = [0,0,0,0,0,0]
# overs_bowled = [0,0,0,0,0,0]
# runs_scored[i] += bat_score
# over_batted[i] += ovrs_played
# runs_conceded [i] += opp_score
# overs_bowled [i] += opp_over
# For = runs_scored[i] / over_batted[i]
# Against = runs_conceded [i] / overs_bowled [i]
# NRR = For - Against
# Table[i][6] = NRR
for row in Table:
print(row)
print('\n')
#main code which asks user input whether he wants to update('Entry') or just check the standings ('Check')
Table = [['Name','Played','Won','Lost','Draws','Points','Net_RR'],['Team1',0,0,0,0,0,0],['Team2',0,0,0,0,0,0],['Team3',0,0,0,0,0,0],['Team4',0,0,0,0,0,0],['Team5',0,0,0,0,0,0],['Team6',0,0,0,0,0,0]]
def table( ):
q = input('what do you want to do:')
if q == 'Entry':
n = input('Name:')
if n == 'Team1':
i = 1
elif n == 'Team2':
i = 2
elif n == 'Team3':
i = 3
elif n == 'Team4':
i = 4
elif n == 'Team5':
i = 5
elif n == 'Team6':
i = 6
net_rr(n,i)
elif q == 'Check':
for row in Table:
print(row)
print('\n')
table()
输出:
what do you want to do:Entry
Name:Team6
Won(w)/Lost(l)/Draw(d):w
batting score:3
over batted:2
opponent score:1
opponent over:3
['Name', 'Played', 'Won', 'Lost', 'Draws', 'Points', 'Net_RR']
['Team1', 0, 0, 0, 0, 0, 0]
['Team2', 0, 0, 0, 0, 0, 0]
['Team3', 0, 0, 0, 0, 0, 0]
['Team4', 0, 0, 0, 0, 0, 0]
['Team5', 0, 0, 0, 0, 0, 0]
['Team6', 1, 1, 0, 0, 3, 0]
关于python - 用于创建和显示联赛表的Python代码,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55551002/