What is wrong in My Program?

“”"
The UEFA Champions League is the most prestigious annual sports competition in
the world. In the group stage of this competition, European football clubs are
divided into 8 groups; there are four teams in each group. The teams in each
group are ranked based on the matches they play against each other, according
to the following rules:

Based on the results of matches, teams are awarded points. Each football match
is played between a home team and an away team. If one of the teams scores more
goals than the other, this team gains 3 points and the other team gains 0
points. In case of a tie (if both teams score the same number of goals),
each of those teams gains 1 point.
The goal difference of a team is the number of goals it scored minus the
number of goals it received, regardless if it scored/received them as the
home team or as the away team.
Between any two teams, the team with more points is ranked higher.
If they have the same number of points (in case of a tie), the team with higher
goal difference is ranked higher.
Each team plays two matches against every other team in its group ― one match
as the home team and one match as the away team. You are given the number of
goals scored by each team for all twelve matches in one group. Determine the
leaders of this group ― the first and second top-ranked team. It is guaranteed
that there are no ties for either of these places (for the given results of the
matches).

Input
The first line of the input contains a single integer T denoting the number of
test cases. The description of T test cases follows.
For each test case, 12 lines follow. Each of these lines describes the result
of one match in the format HomeTeamName HomeTeamGoals vs. AwayTeamGoals
AwayTeamName, where HomeTeamName and AwayTeamName are strings and HomeTeamGoals
and AwayTeamGoals are integers denoting the number of goals scored by the
respective teams in this match.
Output
For each scenario, print a single line containing two space-separated strings ―
the name of the top-ranked team and the name of the second top-ranked team.

Constraints
1≤T≤50
the length of the name of each team does not exceed 10
the name of each team contains only lowercase English letters
0≤ number of goals scored by each team ≤100
Example Input
2
manutd 8 vs. 2 arsenal
lyon 1 vs. 2 manutd
fcbarca 0 vs. 0 lyon
fcbarca 5 vs. 1 arsenal
manutd 3 vs. 1 fcbarca
arsenal 6 vs. 0 lyon
arsenal 0 vs. 0 manutd
manutd 4 vs. 2 lyon
arsenal 2 vs. 2 fcbarca
lyon 0 vs. 3 fcbarca
lyon 1 vs. 0 arsenal
fcbarca 0 vs. 1 manutd
a 3 vs. 0 b
a 0 vs. 0 c
a 0 vs. 0 d
b 0 vs. 0 a
b 4 vs. 0 c
b 0 vs. 0 d
c 0 vs. 0 a
c 0 vs. 0 b
c 1 vs. 0 d
d 3 vs. 0 a
d 0 vs. 0 b
d 0 vs. 0 c
Example Output
manutd fcbarca
d b
Explanation
Example case 1: The total number of points and goal difference for each team
is as follows:

manutd: 16 points, goal difference 12
fcbarca: 8 points, goal difference 4
arsenal: 5 points, goal difference −5
lyon: 4 points, goal difference −11
Example case 2: The total number of points and goal difference for each team
is as follows:

d: 7 points, goal difference 2
b: 7 points, goal difference 1
a: 7 points, goal difference 0
c: 7 points, goal difference −3
Note that in this test case, all teams have the same number of points, but
teams with higher goal difference are ranked higher.
“”"

import pandas as pd
def main():

T = 0
MatchStatus = ""
T = input()
testcase = 0
T = int(T)
M_list = []
Result = []
while(testcase < T):

    testcase += 1
    M_list = []
    MatchStatus = ""
    
    if(T <= 50):
        
        for i in range(0,12):
            
            MatchStatus = input(MatchStatus)
            M_list.append(Perform_String_Operations(MatchStatus))
            MatchStatus = ""

    df = pd.DataFrame(M_list,columns = ['Team A','Score A','VS','Score B'
                        ,'Team B','GoalDifference A','GoalDifference B'])

    dfScores = df.loc[:,['Team A','Score A','Team B','Score B','GoalDifference A','GoalDifference B']]
    Result.append(Calculate_final_Score(dfScores))
    
for items in Result:
    print(items)

def Calculate_final_Score(MatchTable):

ScoreA = 0
GoalA = 0

tempTA = ''

Score_List = []

Teams = getTeamslist(MatchTable)

for teams in Teams:
    ScoreA = 0
    GoalA = 0
    tempTA = teams

    for columns in range(12):
        
        if(MatchTable.loc[columns,'Team A'] == tempTA):
            ScoreA += int(MatchTable.loc[columns,'Score A'])
            GoalA += int(MatchTable.loc[columns,'GoalDifference A'])
        
        if(MatchTable.loc[columns,'Team B'] == tempTA):
            ScoreA += int(MatchTable.loc[columns,'Score B'])
            GoalA  += int(MatchTable.loc[columns,'GoalDifference B'])
            
    Score_List.append([teams,int(ScoreA),int(GoalA)])       

dat = pd.DataFrame(Score_List,columns = ['Teams','Final Score','Goal Difference'])

dat = dat.sort_values(by = ['Final Score'], ascending = False)
if(dat.iloc[0,1] == dat.iloc[1,1]):
    dat = dat.sort_values(by = ['Goal Difference'], ascending = False)
    
dat = dat.iloc[0:2,:]

return str(dat.iloc[0,0]) + " " + str(dat.iloc[1,0])

def getTeamslist(MatchTable):

team = ''
Teams = []
for columns in range(12):
    
    team = MatchTable.loc[columns,'Team A']

    if team not in Teams:
        Teams.append(team)

team = ''        
for columns in range(12):
    
    team = MatchTable.loc[columns,'Team B']

    if team not in Teams:
        Teams.append(team)

return Teams

def Perform_String_Operations(MatchStatus):

match = MatchStatus.split()
ScoreA = 0
ScoreB = 0
    
GoalDifferenceA = 0
GoalDifferenceB = 0
    
ScoreA = int(match[1])
ScoreB = int(match[3])
    
if(ScoreA > ScoreB):
    GoalDifferenceA = ScoreA - ScoreB
    GoalDifferenceB = ScoreB - ScoreA
    match[1] = 3
    match[3] = 0
    match.append(GoalDifferenceA)
    match.append(GoalDifferenceB)
        
if(ScoreB > ScoreA):
    GoalDifferenceA = ScoreA - ScoreB
    GoalDifferenceB = ScoreB - ScoreA
    match[1] = 0
    match[3] = 3
    match.append(GoalDifferenceA)
    match.append(GoalDifferenceB)
    
if(ScoreA == ScoreB):
    match[1] = 1
    match[3] = 1        
    match.append(GoalDifferenceA)
    match.append(GoalDifferenceB)
        
return match