In: Computer Science
Using Python 3,
Write a class called GolfScore that keeps track of the score for a person playing a round of golf. We will limit the round to 9 holes, just to make testing easier. As a reminder, the holes on the golf course are numbered 1 – 9.
Create a class level variable named NUM_OF_HOLES set to 9.
Methods to be written:
The constructor – sets the score for all holes to -1
addScore (hole, score) – this method sets the score for the specified hole. This method should not accept any value if the number of the hole is greater than NUM_OF_HOLES (the round is finished, why would be allow any more scores to be entered?) or less than 1.
getScore (hole) – returns the score from the specified hole. Returns -1 if the hole has not been golfed/entered or if the specified hole is not in the valid range of 1 through 9.
getTotalScore() – returns the total score of the round so far.
getNumHolesPlayed() – returns the number of complete holes so far.
winning(otherGolfer) – compares “your” score to the total of the other golfer. Return True if your score is less than the score of the other golfer. Otherwise return False. Remember, in golf a small score is desired. You can assume that the other golfer than entered the same number of scores.
losing(otherGolfer) – compares “your” score to the total of the other golfer. Return True if your score is greater than the score of the other golfer. Otherwise return False. You can assume that the other golfer than entered the same number of scores.
continueRound() – returns True is the number of rounds completed/entered is less than the variable NUM_OF_HOLES. Otherwise returns False.
avgScore() – returns the average of the holes played. For example, if 6 holes have been played and the total score so far is 45, this method would return 7.5.
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
#code
#required class
class GolfScore:
#class variable for storing number of
holes
NUM_OF_HOLES=9
#constructor
def __init__(self):
#initializing a list
containing NUM_OF_HOLES number of values, all initialized to
-1
self.__scores=[-1
for i in
range(self.NUM_OF_HOLES)]
#adds a Score to a hole
def addScore(self, hole,
score):
#checking if hole
value is valid and has not set before
if
hole>=1 and hole<=self.NUM_OF_HOLES
and self.__scores[hole-1]==-1:
#setting score to specified hole
self.__scores[hole-1]=score
#returns the score for a hole
def getScore(self,
hole):
#validating
hole
if
hole>=1 and hole<=self.NUM_OF_HOLES:
#returning the value
return self.__scores[hole-1]
return
-1 #invalid hole
#returns the total score
def getTotalScore(self):
s=0
#summing up all
scores that are not -1
for i in self.__scores:
if i!=-1:
s+=i
return
s
#returns the number of holes played
def
getNumHolesPlayed(self):
c = 0
#counting the number
of holes that has score not equal to -1
for i in self.__scores:
if i != -1:
c += 1
return
c
#returns True if score < other golfer's
score
def
winning(self,otherGolfer):
return
self.getTotalScore()<otherGolfer.getTotalScore()
# returns True if score > other golfer's
score
def
losing(self,otherGolfer):
return
self.getTotalScore()>otherGolfer.getTotalScore()
#returns True if there are more holes to
play
def
continueRound(self):
return
self.getNumHolesPlayed()<self.NUM_OF_HOLES
#returns the average score
def avgScore(self):
avg=0
#finding average
only if NumHolesPlayed is greater than 0
if
self.getNumHolesPlayed()>0:
avg=self.getTotalScore()/self.getNumHolesPlayed()
return
avg