I added docstrings to the Yahtzee.py

When writing docstrings, I had to think deeper about what is happening at each line of the code and how it effects the final result.
Some methods had choices, so I had to run through how they made a choice.
When writing code, I have to also go bit by bit, but actually write out the code to make it work, rather than just "walking through it" when writing the docstrings.
Yes! I think it is helpful, especaily for longer codes. It can get a bit too much when there is a lot going on, so having doscstrings can help organize!
This commit is contained in:
mbhatti4
2025-11-11 14:59:09 -05:00
parent f4b2d95e02
commit 0d3853c89f

View File

@@ -5,11 +5,16 @@ class Yahtzee:
This version of Yahtzee is initialized with a list of goals. This version of Yahtzee is initialized with a list of goals.
""" """
def __init__(self, goals): def __init__(self, goals):
""" This is the initial score keeper of the instances """
self.score = 0 self.score = 0
self.goals = goals self.goals = goals
self.dice = [Die() for num in range(5)] self.dice = [Die() for num in range(5)]
def play(self): def play(self):
""" Play an entire game.
14 Starts by greeting the user, then plays rounds until all the goals
15 have been used. When the game is over, tells the player their final
16 score"""
print("Welcome to Yachtzee!") print("Welcome to Yachtzee!")
self.score = 0 self.score = 0
for goal in self.goals: for goal in self.goals:
@@ -19,6 +24,7 @@ class Yahtzee:
print(f"Your final score was {self.score}") print(f"Your final score was {self.score}")
def play_round(self): def play_round(self):
""" """
print("=" * 80) print("=" * 80)
self.rolls_left = 3 self.rolls_left = 3
for die in self.dice: for die in self.dice:
@@ -29,10 +35,12 @@ class Yahtzee:
self.score += goal.score(self.dice) self.score += goal.score(self.dice)
def show_status(self): def show_status(self):
"""prints the self score, the amount of rolls left and dice"""
dice = ', '.join([str(die) for die in self.dice]) dice = ', '.join([str(die) for die in self.dice])
print(f"Score: {self.score}. Rolls left: {self.rolls_left}. Dice: {dice}.") print(f"Score: {self.score}. Rolls left: {self.rolls_left}. Dice: {dice}.")
def choose_goal(self): def choose_goal(self):
""" Allows the user to choose goal, and the nappends that goal into their options"""
options = [] options = []
unused_goals = self.get_unused_goals() unused_goals = self.get_unused_goals()
for goal in unused_goals: for goal in unused_goals:
@@ -49,6 +57,8 @@ class Yahtzee:
return unused_goals[choice] return unused_goals[choice]
def get_choice(self, options): def get_choice(self, options):
""" asks player what they would liek to do,
keeps track of their choice, and if not valid, informs them """
print("What would you like to do?") print("What would you like to do?")
for i, option in enumerate(options): for i, option in enumerate(options):
print(f"{i}. {option}") print(f"{i}. {option}")
@@ -59,6 +69,7 @@ class Yahtzee:
return int(choice) return int(choice)
def option_choice_is_valid(self, choice, options): def option_choice_is_valid(self, choice, options):
""" """
if not choice.isdigit(): if not choice.isdigit():
return False return False
if int(choice) < 0: if int(choice) < 0:
@@ -68,6 +79,7 @@ class Yahtzee:
return True return True
def count_unused_goals(self): def count_unused_goals(self):
""" counts how much goals are left unused"""
return len(self.get_unused_goals()) return len(self.get_unused_goals())
def get_unused_goals(self): def get_unused_goals(self):