我们正在做计算机科学课的一项活动,而且我很难理解几行代码的含义。这些代码行是做什么的? (运动模拟)
这里是最初的代码(对于任何你可能需要的背景信息)。
class SportsMatch(object):
def __init__(self, teamA="Team A", teamB="TeamB"):
self.teamA = teamA
self.scoreA = 0
self.teamAScorePoints = 1
self.teamB = teamB
self.scoreB = 0
self.teamBScorePoints = 1
def setScorePoints(self, teamAScorePoints=1, teamBScorePoints=1):
self.teamAScorePoints = teamAScorePoints
self.teamBScorePoints = teamBScorePoints
def whoWins(self):
if (self.scoreA < self.scoreB):
print(self.teamB+" win the game!")
elif (self.scoreA > self.scoreB):
print(self.teamA+" win the game!")
else:
print("Tie score")
def teamAScores(self):
self.scoreA = self.scoreA + self.teamAScorePoints
def teamBScores(self):
self.scoreB = self.scoreB + self.teamBScorePoints
然后,我们应该考虑下面的代码,弄清每一行代码的作用:
s = SportsMatch("Chargers", "Raiders")
s.setScorePoints(1, 2)
s.teamAScores()
s.teamBScores()
s.teamAScores()
s.teamBScores()
s.whoWins()
我有一种大致的了解,但我的老师要我们更具体。我也明白第二行是用参数1和2调用的,但我不确定这些数字是如何影响其余代码的。如果有人能帮我解释这几行代码,那将不胜感激!
是体育*刺激*还是*模拟*? :P –