number of goals scored by a football team. The NumberOfGoals class should contai
ID: 3853070 • Letter: N
Question
number of goals scored by a football team. The NumberOfGoals
class should contain a single integer as instance data, representing
the number of goals scored. Write a constructor to initialize
the number of goals to zero. Write a method called setGoal
that increments the value by one whenever a goal is scored,
and another method called getGoal that returns the total number
of goals scored so far. Finally, create a driver class called
GoalTracker that creates a few NumberOfGoals objects and tests
their methods.
Explanation / Answer
class NumberOfGoals:
def __init__(self,goals = 0):
self.goals = goals
def setGoal(self):
self.goals += 1
def getGoal(self):
print("No.of Goals Scored",self.goals)
if __name__ == '__main__':
x= NumberOfGoals(4)
y = NumberOfGoals(3)
x.setGoal()
y.setGoal()
x.getGoal()
y.getGoal()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.