Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

How to fix “TypeError: 'NoneType' object is not subscriptable” in Python

I recently started learning of Object-Oriented Programming in Python. It's a very hard concept. In order to practice I've decided to create a football match simulator game using all of concepts I've learned such as Inheritance and Composition.

I'm having a problem with my project. I created the attacking_team instance that was instantiated as None in the constructor but later modified in the randomness method of the same class (called Engine) to store a list.

Then, I have a class called SetPieces that inherits from the Engine class so the SetPiece class should have all of the instances in the Engine class (including the attacking_team instance).

Whenever I call the SetPieces class which stores the value of attacking_team[15] in the instance fk_taker I get "TypeError: 'NoneType' object is not subscriptable" which means that something like this is happening: None[0], but the value of attacking_team should have been changed from None to the list I assigned to it in the randomness method,

import random

team1 = []
team2 = []

class Teams:
    # La Liga Teams
    barcelona = ["Barcelona", "Messi", "Suarez", "Dembele", "Coutinho", "Busquets", "Vidal", "Semedo", "Umtiti",
             "Pique", "Alba", "Ter Stegen", "Arthur", "Rakitic", "Roberto", "Messi", "Messi", "Suarez"]
    real_madrid = ["Real Madrid", "Asensio", "Benzema", "Bale", "Kroos", "Casemiro", "Modric", "Marcelo", "Varane",
               "Ramos", "Carvajal", "Courtois", "Isco", "Vazquez", "Ceballos", "Bale", "Modric", "Ramos"]
    teams = {'BARCELONA': barcelona, 'REAL MADRID': real_madrid
         }

    def __init__(self, home, away):
        global team1, team2
        self.home = home
        self.away = away

        team1 = Teams.teams.get(self.home.upper(), "Enter a team from the list!")
        team2 = Teams.teams.get(self.away.upper(), "Enter a team from the list!")

This class handles the teams that you can choose from to play. The list for each team is in the order of: name of team (at index 0), the players in the starting eleven (so from index 1 to 11), 3 substitutes (index 12 to 14), freekick taker (index 15), cornerkick tacker (index 16), penalty taker (index 17).

class Engine:
    def __init__(self):
        self.defender = None
        self.gk = None
        self.player = None
        self.attacking_team = None
        self.position = None
        self.attackers = None
        self.midfielders = None
        self.cbs = None
        self.rb = None
        self.lb = None

    def randomness(self):
        teams = [team1, team2]
        self.attacking_team = random.choice(teams)

        self.attackers = self.attacking_team[1:4]
        self.midfielders = self.attacking_team[4:7]
        self.cbs = self.attacking_team[8:10]
        self.rb = self.attacking_team[10]
        self.lb = self.attacking_team[7]
        team_possession = [self.attackers, self.midfielders, self.rb, self.lb, self.cbs]
        self.position = random.choice(team_possession)
        self.player = random.choice(self.position)

        if self.player in team1:
            self.defender = random.choice(team2[7:11])
            self.gk = team2[11]
        else:
            self.defender = random.choice(team1[7:11])
            self.gk = team1[11]

This is the class I was referring to that has the attacking_team instance. Note, the randomness function was called in another class called MatchEvent

class MatchEvent:
    def __init__(self):
        self.engine = Engine()
    # deals with the randomness of the events
    def play(self):
        self.engine.randomness()

I created an object for the class by doing the following:

Teams("Barcelona", "Real Madrid")
match = MatchEvent()
match.play()

I get the type error when this class run:

class SetPieces(Engine):
    def __init__(self):
        super().__init__()
        self.fk_taker = self.attacking_team[15]
        self.ck_taker = self.attacking_team[16]
        self.pk_taker = self.attacking_team[17]

More details of error:

Traceback (most recent call last):
  File "C:/Users/red-parrot@hotmail/Documents/Python Files/Soccer Match Siimulator/SoccerMatchSim.py", line 250, in <module>
    match = MatchEvent()
  File "C:/Users/red-parrot@hotmail/Documents/Python Files/Soccer Match Siimulator/SoccerMatchSim.py", line 78, in __init__
    self.foul = Foul()
  File "C:/Users/red-parrot@hotmail/Documents/Python Files/Soccer Match Siimulator/SoccerMatchSim.py", line 139, in __init__
    self.fk_taker = self.attacking_team[15]
TypeError: 'NoneType' object is not subscriptable

NOTE: This is not all of the code. I just picked out parts I think is relevant to the error. Respond if you need extra info.

Comments