Home > other >  Python problem involving 2 For loops that I can't work out
Python problem involving 2 For loops that I can't work out

Time:12-01

I am fairly new to python.

I am reading in a CSV file that contains the stats from every english premier league match in 2018/19.

I have created a list of all of the teams. I am then trying to take each team in turn and loop through all of the matches to calculate each teams total points for the season.

It seems to work for the first team. It takes Man Utd and I get the correct points for them. The problem I have is getting to the next team in the list and then looping through the points code with them.

import csv

with open('EPL1819.csv') as file:
    eplgames = csv.DictReader(file)

    teampoints = list()
    eplteams = list()
    teamcount = 0
    count = 0
    # Outer loop going through teams one at a time

    for i in range(20):
        points = 0
        # Inner loop going through each match

        for x in eplgames:

            # Populates the eplteams list
            if x['HomeTeam'] not in eplteams:
                eplteams.append(x['HomeTeam'])
                teamcount  = 1

            #print(eplteams[i])
            # Works out the match result
            if x['FTHG'] > x['FTAG']:
                match_result = x['HomeTeam']
            elif x['FTHG'] < x['FTAG']:
                match_result = x['AwayTeam']
            else:
                match_result = "Draw"

            if eplteams[i] == match_result:
                points  = 3

            if eplteams[i] == x['HomeTeam']:
                if match_result == "Draw":
                    points  = 1

            if eplteams[i] == x['AwayTeam']:
                if match_result == "Draw":
                    points  = 1


        # Populates the teampoints list
        teampoints.append(points)

        print(eplteams[i])
        print("Points:", points)
        print("Points List:", teampoints[i])

CodePudding user response:

You are adding teams to eplteams based on some condition:

if x['HomeTeam'] not in eplteams:
    eplteams.append(x['HomeTeam'])
    teamcount  = 1

And then using eplteam element in condition:

if eplteams[i] == match_result:
    points  = 3

if eplteams[i] == x['HomeTeam']:
    if match_result == "Draw":
        points  = 1

if eplteams[i] == x['AwayTeam']:
    if match_result == "Draw":
        points  = 1

It doesn't look right. Element i in eplteams may not exist at that moment.

CodePudding user response:

You need to populate before your loop through the eplgames or else it might not find the teams that are playing:

def populate(eplgames):
    eplteams = []
    for x in eplgames:
        # Populates the eplteams list
        if x['HomeTeam'] not in eplteams:
            eplteams.append(x['HomeTeam'])
            teamcount  = 1
    return teamcount, eplteams

populate before loop:

teamcount, eplteams = populate(eplgames)
for i in range(20):
    points = 0
    # Inner loop going through each match
    for x in eplgames:
        #print(eplteams[i])
        # Works out the match result
        if x['FTHG'] > x['FTAG']:
            match_result = x['HomeTeam']
  • Related