Skip to content

Fix consecutive-wins bug in solution - #17

Open
XueyanZhang wants to merge 7 commits into
spiside:masterfrom
XueyanZhang:fix-solution-runner
Open

Fix consecutive-wins bug in solution#17
XueyanZhang wants to merge 7 commits into
spiside:masterfrom
XueyanZhang:fix-solution-runner

Conversation

@XueyanZhang

Copy link
Copy Markdown

Problem

runner.consecutive_wins += 1

consecutive_wins is never initialized.
The first time the player guesses correctly, the reference solution
crashes with:

AttributeError: 'GameRunner' object has no attribute 'consecutive_wins'

Fix

  • Track consecutive wins with a local consecutive_wins variable in
    run() (initialized to 0), removing the broken instance attribute.
  • Roll the dice each round so the game state actually changes between
    rounds. (I'm not sure if not re-rolling is intentional — happy to revert this if so.)

- remove uninitialized `runner.consecutive_wins` attribute
- roll the dice each round

@leespen1 leespen1 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pull request correctly changes the uninitialized runner.consecutive_wins to a function-local variable so that the program does not crash, and changes count to consecutive_wins so there is no redundancy (count is used to keep track of the number of consecutive wins).

This pull request also re-rolls the dice each round, so that the answer is not the same every time.

@rogerlucena rogerlucena left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks for opening this PR @XueyanZhang !

Also, it is nice indeed to re-roll the dice on each iteration to make the gameplay more interesting - leveraging the already implemented roll(dice) function from the die module.

Finally, just one quick note: given that the counters for wins and loses (sic) are inside the runner object as attributes, wouldn't it be a good practice/idea to keep that pattern of the file and also let the consecutive_wins counter inside the runner object (as the previous buggy code was hinting)?

If you agree, I have also added the changes as suggestions below. This way, to avoid the previous bug we can just initialize self.consecutive_wins = 0 inside reset(self) (as it is done for the other counters for that run).

Thanks again for spotting this bug!

Comment thread solutions/dicegame/runner.py Outdated
Comment thread solutions/dicegame/runner.py Outdated
Comment thread solutions/dicegame/runner.py Outdated
Comment thread solutions/dicegame/runner.py Outdated
Comment thread solutions/dicegame/runner.py Outdated
Comment thread solutions/dicegame/runner.py
XueyanZhang and others added 6 commits August 29, 2026 15:29
as per suggestion from @@rogerlucena

Co-authored-by: Roger Leite Lucena <27161330+rogerlucena@users.noreply.github.com>
Co-authored-by: Roger Leite Lucena <27161330+rogerlucena@users.noreply.github.com>
Co-authored-by: Roger Leite Lucena <27161330+rogerlucena@users.noreply.github.com>
Co-authored-by: Roger Leite Lucena <27161330+rogerlucena@users.noreply.github.com>
Co-authored-by: Roger Leite Lucena <27161330+rogerlucena@users.noreply.github.com>
Co-authored-by: Roger Leite Lucena <27161330+rogerlucena@users.noreply.github.com>
@XueyanZhang

Copy link
Copy Markdown
Author

@rogerlucena Great catch! I have revised per your suggestion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants