I'm a freshman in the fantastic world of python and at the moment I'm struggling with this problem... That's an example of what I've coded:
class League():
def __init__(self, teams=[]):
self.teams = teams
def initLeague(self):
for a in range(2):
self.teams.append(Team())
self.teams[a].name = "Team" + str(a)
for b in range(3):
self.teams[a].players.append(Player())
self.teams[a].players[b].name = "Name-" + str(a) + "-" + str(b)
def printLeague(self):
for team in self.teams:
print(team.name)
for player in team.players:
print(player.name)
class Team():
def __init__(self, name=None, players=[]):
self.name = name
self.players = players
class Player():
def __init__(self, name=None):
self.name = name
nba = League()
nba.initLeague()
nba.printLeague()
The output looks like that:
Team0
Name-1-0
Name-1-1
Name-1-2
None
None
None
Team1
Name-1-0
Name-1-1
Name-1-2
None
None
None
[Finished in 0.051s]
So I would like to know where do these None come from? I noticed they depend on range(n)... it's like if the 'for a' loop is repeating inside the 'for b' loop. Another problem is that the first part of the output should be:
Team0
Name-0-0
Name-0-1
Name-0-2
...
Could someone help me? Thank you!