我们在计算机科学课上做一个活动,我很难理解几行代码是什么意思。你知道吗
以下是初始代码(对于您可能需要的任何背景信息)。你知道吗
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调用的,但我不确定这些数字在代码的其余部分中是如何起作用的。如果有人能帮我解释最后几行代码,我将不胜感激!你知道吗
设置初始变量:
这两个是分数增量:
用于增加每个团队的得分:
现在是流程:
代码的一般说明:
这行代码从
SportsMatch
类调用__init__
方法,并传递方法“Chargers”和“Raiders”。然后将它们保存为运动队的名称。你知道吗此行从类中调用
setScorePoints
方法,并将其传递给1
和2
。这些值被保存为每个团队得分将在得分时增加的金额。你知道吗这些行调用
teamAScores
和teamBScores
方法。这些方法根据调用哪个方法来增加团队的得分。你知道吗这将调用类的
whoWins
方法,该方法比较团队得分并打印获胜团队。你知道吗获胜的队伍将是B队,也称为
Raiders
。B队得分4
,a队得分2
。你知道吗相关问题 更多 >
编程相关推荐