我正在编写一个程序,您可以将其放入主队,客队以及游戏结果中。我希望团队的数据能够根据这一点进行更改,而且大部分都是如此。但是我无法让“点数”,“净胜球”和“玩”(游戏)改变!这是我到目前为止写的代码:
class team:
def __init__(self, name, wins, drawn, losses, goals_for, goals_against):
self.name = name
self.wins = int(wins)
self.drawn = int(drawn)
self.losses = int(losses)
self.goals_for = int(goals_for)
self.goals_against = int(goals_against)
self.goals_difference = (self.goals_for - self.goals_against)
self.points = ((self.wins * 3) + self.drawn)
self.played = (self.wins + self.drawn + self.losses)
def __repr__(self):
return 'Name:{} P:{} W:{} D:{} L:{} GF:{} GA:{} GD:{} PTS:{}'.format(self.name, self.played, self.wins, self.drawn, self.losses, self.goals_for, self.goals_against, self.goals_difference, self.points)
detroit_red_wings = team("Detroit", 1, 0, 3, 4, 5)
los_angeles_kings = team("LA", 0, 1, 4, 3, 7)
toronto_maple_leafs = team("Toronto", 1, 2, 2, 3, 6)
teamlist = [detroit_red_wings, los_angeles_kings, toronto_maple_leafs]
print(teamlist)
class data_input:
def home_team_input(self):
home_team = input("Type in the home team: ")
for i in teamlist:
if i.name == home_team:
return i
def away_team_input(self):
away_team = input("Type in the away team: ")
for t in teamlist:
if t.name == away_team:
return t
def result_input(self):
goals_home_team = int(input("Type in the number of goals made by the home team: "))
goals_away_team = int(input("Type in the number of goals made by the away team: "))
return (goals_home_team, goals_away_team)
def adding_result():
home_team = data_input.home_team_input()
away_team = data_input.away_team_input()
goals_home_team, goals_away_team = data_input.result_input()
home_team.goals_for += goals_home_team
home_team.goals_against += goals_away_team
away_team.goals_for += goals_away_team
away_team.goals_against += goals_home_team
if goals_home_team > goals_away_team:
home_team.wins += 1
away_team.losses += 1
if goals_home_team < goals_away_team:
away_team.wins += 1
home_team.losses += 1
if goals_home_team == goals_away_team:
home_team.drawn += 1
away_team.drawn += 1
data_input = data_input()
adding_result()
print(teamlist)
我在课程__init__
的{{1}}方法中编写了属性说明,您可以看到这些点取决于team
。这一切都在我创建对象时有效,但当我输入新游戏的结果时,wins
不会改变(points
或played
也不会)。这让我感到惊讶,因为当我在goals_difference
函数中输入游戏结果时,其他属性会发生变化。
答案 0 :(得分:3)
如果更新team
类以生成计算字段属性,则属性函数将始终返回正确的结果。如果您尝试设置这些属性,也会出现错误,因为它们不可设置,即它们是对其他设置数据进行计算的结果。
class team:
def __init__(self, name, wins, drawn, losses, goals_for, goals_against):
self.name = name
self.wins = int(wins)
self.drawn = int(drawn)
self.losses = int(losses)
self.goals_for = int(goals_for)
self.goals_against = int(goals_against)
@property
def goals_difference(self):
return self.goals_for - self.goals_against
@property
def points(self):
return self.wins * 3 + self.drawn
@property
def played(self):
return self.wins + self.drawn + self.losses
def __repr__(self):
return 'Name:{} P:{} W:{} D:{} L:{} GF:{} GA:{} GD:{} PTS:{}'.format(
self.name, self.played, self.wins, self.drawn, self.losses,
self.goals_for, self.goals_against, self.goals_difference,
self.points)
我还会考虑制作W / L / D和GF / GA初始化程序的tupples或词典,而不是将5个变量传递给初始化程序。
答案 1 :(得分:0)
points
,goals_difference
和played
不会更新,因为更新其他属性后,__init__
方法不会再次运行 - 您只是使用新值明确更新其他属性。请注意,这些不更新的属性由值分配,而不是通过引用分配,除非您明确这样做,否则您的对象无法知道应更新这些属性。
您可以做几件事 - 一个简单的选项可能是提供一种更新聚合属性的方法,您可以在显式更改其他属性后调用。我会将所有属性简单地作为其他属性的数学公式(所以points
,goals_difference
和played
),并从初始化方法中删除它们。如果你真的需要这些看起来像这个
update_aggregates
这样的小方法
def update_aggregates(self):
self.goals_difference = (self.goals_for - self.goals_against)
self.points = ((self.wins * 3) + self.drawn)
self.played = (self.wins + self.drawn + self.losses)
编辑:忽略我的简单尝试,Steve Cohen提出的解决方案无论如何都更好 - 使用@property可确保您的值始终适当更新,而无需手动调用