I wrote a docstring in each function about what that function does.

When I was writing the docstring I was thinking about how to explain it rather than trying to make sure the program is working.
I might use a doctring in my suture projects to make sure that I know what the function is for in case I need to go back and fix things.
This commit is contained in:
juddin2
2025-11-09 19:53:11 -05:00
parent 800b6671c3
commit 4b96a94574

View File

@@ -5,34 +5,39 @@ class Yahtzee:
This version of Yahtzee is initialized with a list of goals.
"""
def __init__(self, goals):
self.score = 0
self.goals = goals
self.dice = [Die() for num in range(5)]
"""Sets up the game with goals, score, and 5 dice."""
self.score = 0
self.goals = goals
self.dice = [Die() for num in range(5)]
def play(self):
print("Welcome to Yachtzee!")
self.score = 0
for goal in self.goals:
goal.used = False
while self.count_unused_goals() > 0:
self.play_round()
print(f"Your final score was {self.score}")
"""Play the entire game until all goals are used."""
print("Welcome to Yachtzee!")
self.score = 0
for goal in self.goals:
goal.used = False
while self.count_unused_goals() > 0:
self.play_round()
print(f"Your final score was {self.score}")
def play_round(self):
print("=" * 80)
self.rolls_left = 3
for die in self.dice:
"""Play one round: roll dice, choose a goal and make a score."""
print("=" * 80)
self.rolls_left = 3
for die in self.dice:
die.roll()
self.show_status()
goal = self.choose_goal()
goal.used = True
self.score += goal.score(self.dice)
self.show_status()
goal = self.choose_goal()
goal.used = True
self.score += goal.score(self.dice)
def show_status(self):
"""Shows the recent score."""
dice = ', '.join([str(die) for die in self.dice])
print(f"Score: {self.score}. Rolls left: {self.rolls_left}. Dice: {dice}.")
def choose_goal(self):
"""Players should pick a goal or roll again."""
options = []
unused_goals = self.get_unused_goals()
for goal in unused_goals:
@@ -49,6 +54,7 @@ class Yahtzee:
return unused_goals[choice]
def get_choice(self, options):
"""Players get to choose an option."""
print("What would you like to do?")
for i, option in enumerate(options):
print(f"{i}. {option}")
@@ -59,6 +65,7 @@ class Yahtzee:
return int(choice)
def option_choice_is_valid(self, choice, options):
"""Checks if the choice made is correct."""
if not choice.isdigit():
return False
if int(choice) < 0:
@@ -71,6 +78,7 @@ class Yahtzee:
return len(self.get_unused_goals())
def get_unused_goals(self):
"""Players get back there unused goals."""
unused_goals = []
for goal in self.goals:
if not goal.used:
@@ -78,6 +86,7 @@ class Yahtzee:
return unused_goals
def reroll(self):
"""Players get to roll the dice."""
self.rolls_left -= 1
choices = self.get_reroll_choices()
dice_to_reroll = self.get_dice_to_reroll(choices)
@@ -85,6 +94,7 @@ class Yahtzee:
die.roll()
def get_dice_to_reroll(self, choice_ints):
"""Players get to re-roll there dice based on there choice."""
dice_to_reroll = []
for die in self.dice:
if die.face in choice_ints:
@@ -93,6 +103,7 @@ class Yahtzee:
return dice_to_reroll
def get_reroll_choices(self):
"""Gives players options on which dice to re-roll."""
print("Which dice do you want to re-roll?")
choices = input("> ")
while not self.reroll_choices_are_valid(choices):
@@ -102,6 +113,7 @@ class Yahtzee:
return choice_ints
def reroll_choices_are_valid(self, choices_str):
"""Checks if the re-roll dice are correct."""
if not choices_str.isdigit():
return False
choice_ints = [int(digit) for digit in choices_str]