Вывод этого скрипта должен имитировать матч между 6 футбольными командами, играющими в 10 матчей, отображать общий рейтинг навыков для этой команды и отображать победителя в каждой игре.
Пример:Аресенал 666 против Манчестер Сити 650Игра начинаетсяИгра заканчивается Aresenal 666 побед
Однако при запуске кода в среде IDE появляется следующая ошибка:
Сезон начинаетсяРаунд начинается'<' team.Team объект в 0x006C0EF0> vs. '<' team.Team объект в 0x006C0F30>Игра начинаетсяИгра заканчиваетсяTraceback (последний вызов был последним):Файл «C: / Users / --- / Desktop / Python / main.py», строка 47, в модуле «<»>главный()Файл "C: / Users / --- / Desktop / Python / main.py", строка 40, в основномfirst_league.play_season ()Файл "C: \ Users --- \ Desktop \ Python \ team.py", строка 88, в play_seasonself.play_round ()Файл "C: \ Users --- \ Desktop \ Python \ team.py", строка 108, в play_roundgame.play ()Файл "C: \ Users --- \ Desktop \ Python \ team.py", строка 62, в игреif self.home_team.rating ()> self.away_team.rating ():Файл "C: \ Users --- \ Desktop \ Python \ team.py", строка 25, в рейтингерейтинг + = player.skillAttributeError: у объекта 'NoneType' нет атрибута 'skill'
Есть идеи, как это исправить?
main.py
import random
from player import generate_player
from team import League, Team
def generate_player():
pass
def main():
#set up our data
#generate some players
players = []
for i in range (100):
players.append(generate_player())
#set up 6 teams
teams = [
Team('Chelsea'),
Team('Man City'),
Team('Arsenal'),
Team('West Ham'),
Team('Hull City'),
Team('Swansea'),
]
for team in teams:
for player_num in range(11):
#give them 11 starting players
selected_player = random.choice(players)
team.players.append(selected_player)
players.remove(selected_player)
#we have a single league
first_league = League('Premiership League')
first_league.set_teams(teams)
first_league.play_season()
print('a')
if __name__ == '__main__':
main()
player.py
import random
class Players:
"""
player is on a single team, with many other players
players play in a game for a team
"""
def __init__(self, name):
self.name = name
#player skill rankings
self.skill = skill
def salary(self):
return 50000 + self.skill * 100
def generate_player():
first_names = [
'Sophia', 'Jackson', 'Emma', 'Aiden', 'Olivia', 'Lucas', 'Ava', 'Liam', 'Mia', 'Noah', 'Isabella', 'Ethan',
'Riley', 'Mason', 'Aria', 'Caden', 'Zoe', 'Oliver', 'Charlotte', 'Elijah', 'Lily', 'Grayson', 'Layla', 'Jacob',
'Amelia', 'Michael', 'Emily', 'Benjamin', 'Madelyn', 'Carter', 'Aubrey', 'James', 'Adalyn', 'Jayden',
'Madison', 'Logan', 'Chloe', 'Alexander',
]
first_name = random.choice(first_names)
last_name = random.choice(last_name)
full_name = '{} {}'.format(first_name, last_name)
# generate skill and salary
skill = 10 + random.randint(0, 90)
return Player(full_name, skill)
team.py
import copy
import random
class Team:
"""
Team has many players
Team has a ranking in a league
Team plays games against other teams
Team has a single manager
"""
def __init__(self,name):
self.name = name
self.players = []
self.wins = 0
self.losses = 0
def rating(self):
"""
what is the rating of the team
"""
rating = 0
for player in self.players:
rating += player.skill
return rating
def __str__(self):
return '{} {}'.format(self.name, self.rating())
class Manager:
"""
runs a teams
"""
pass
class Game:
"""
plays a game between two teams
game belongs to a league
"""
def __init__(self, league, home_team, away_team):
self.league = league
self.home_team = home_team
self.away_team = away_team
self.home_team_won = None
print('{} vs. {}'.format(self.home_team, self.away_team))
def play(self):
"""
play the game, return who won
True means the home team won, False means the away team won
"""
print ('Play begins')
#insert game here
print ('Play ends')
if self.home_team.rating() > self.away_team.rating():
print ('{} wins'.format(self.home_team))
self.home_team_won = True
else:
print('{} wins.'.format(self.away_team))
self.home_team_won = False
class League:
"""
league has many teams
each team is going to have a ranking within this league
"""
def __init__(self, name):
self.name = name
self.teams = []
def set_teams(self, teams):
self.teams = teams
def play_season(self):
"""
play 10 games between all the teams
"""
print ('Season begins')
for i in range(10):
self.play_round()
print('Season ends')
def play_round(self):
"""
play a round, which is 3 games
"""
print('Round begins')
num_teams = len(self.teams)
num_games = num_teams // 2
teams_to_play = copy.copy(self.teams)
for game_num in range(num_games):
home_team = random.choice(teams_to_play)
teams_to_play.remove(home_team)
away_team = random.choice(teams_to_play)
teams_to_play.remove(away_team)
game = Game(self, home_team, away_team)
game.play()
self.resolve_game(game)
print('Round ends')
#ladder status
self.ladder()
def ladder(self):
for team in sorted(self.teams, key= lambda t: t.wins):
print('{} {} wins'.format(team, team.wins))
def resolve_game(self, game):
if game.home_team_won:
# home team won
game.home_team.wins += 1
game.away_team.losses += 1
else:
#away team won
game.away_team.wins += 1
game.home_team.losses += 1