创建和显示排行榜的Python代码

创建和显示排行榜的Python代码,python,Python,我对编码还不熟悉,只是为了练习,我正在努力为我所在学院的一个友好板球联盟创建一个排行榜。该表将随着联赛的进展而更新。但我还想保留一个选项来检查具有不同列的表排名,但每次我尝试检查排名时,它都返回空表 #function that calculates various parameters def net_rr(n,i): result = input('Won(w)/Lost(l)/Draw(d):') if (result == 'w') or (resul

我对编码还不熟悉,只是为了练习,我正在努力为我所在学院的一个友好板球联盟创建一个排行榜。该表将随着联赛的进展而更新。但我还想保留一个选项来检查具有不同列的表排名,但每次我尝试检查排名时,它都返回空表

#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]
因为您在函数体中定义了“Table”变量,所以无法全局查看它

把它移到你的功能之外,一切都会好起来的

注意:我注释掉了一些行,只是为了测试代码

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]
因为您在函数体中定义了“Table”变量,所以无法全局查看它

把它移到你的功能之外,一切都会好起来的

注意:我注释掉了一些行,只是为了测试代码

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]

感谢马哈茂德·艾尔沙哈特提供的解决方案:)感谢马哈茂德·艾尔沙哈特提供的解决方案:)