Reputation: 2127
I am making a golf scoring program using python 3 which, for each of the 18 holes stores: the hole number, the par, the difficulty rank, and the target score.
The target score is calculated from the par, difficulty, and handicap (which may be changed by the user).
What would you advise to be the best method for storing this data so that it can be displayed in a table-like fashion, and the target score easily changed if the user edits the value of the handicap?
I don't really know where to start as I have very little experience.
Thanks.
Upvotes: 0
Views: 134
Reputation: 54233
Build a class.
class HoleScore(object):
def __init__(self, hole_number, par, difficulty, handicap=0):
self.hole_number = hole_number
self.par = par
self.difficulty = difficulty
self.handicap = handicap
@property
def target_score(self):
return do_some_calculation_of_attributes(self.par, self.difficulty, self.handicap)
Then you can add a few dunder methods to help things along, or (better) design a function to build a table from a bunch of HoleScore
objects. Something like:
# inside class HoleScore
@staticmethod
def make_table(list_of_holes):
"""list_of_holes is a list of HoleScore objects"""
print("Some | headers | here")
for hole in list_of_holes:
fields = [hole.hole_number,
hole.par,
hole.handicap,
hole.target_score]
print("|".join(fields)) # use some string formatting here?
Upvotes: 1