此脚本的输出应模拟6支足球队之间进行10场比赛的对决,显示该球队的整体技术评分,并显示每场比赛的获胜者。
示例:
阿森纳666 vs.曼城650
比赛开始
播放结束
阿森纳666获胜
但是,在运行代码时,IDE会显示以下错误:
季节开始了
回合开始
'<'team.Team对象位于0x006C0EF0>与'<'team.Team对象位于0x006C0F30>
比赛开始
播放结束
追溯(最近一次通话):
'<'module>
中的文件“ C:/ Users / --- / Desktop / Python / main.py”,第47行
main()
main中的文件“ C:/ Users / --- / Desktop / Python / main.py”,第40行
first_league.play_season()
在play_season中的文件“ C:\ Users --- \ Desktop \ Python \ team.py”,第88行
self.play_round()
在play_round中的文件“ C:\ Users --- \ Desktop \ Python \ team.py”,第108行
game.play()
正在播放文件“ C:\ Users --- \ Desktop \ Python \ team.py”,第62行,
如果self.home_team.rating()> self.away_team.rating():
文件“ C:\ Users --- \ Desktop \ Python \ team.py”,第25行,评级为
评分+ = player.skill
AttributeError:'NoneType'对象没有属性'skill'
关于如何解决此问题的任何想法?
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()
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)
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