step 4
TirSystem/github-action: Sync GitHub mirror metadata / sync-metadata (push) Successful in 4s

Keeping track of the player's lives
This commit is contained in:
2026-10-04 12:41:43 +08:00
parent ce87f23314
commit a76df46b79
6 changed files with 91 additions and 38 deletions
+22 -1
View File
@@ -1,4 +1,4 @@
from hangman.constants import WORD_LIST
from hangman.constants import MAX_LIVES, WORD_LIST
from hangman.game import Game, choose_word, is_in_word
@@ -45,3 +45,24 @@ def test_wrong_guesses_do_not_win():
g = Game("hi")
g.guess("x")
assert not g.won
def test_wrong_guess_loses_life():
g = Game("cat")
assert g.lives == MAX_LIVES
g.guess("z")
assert g.lives == MAX_LIVES - 1
def test_correct_guess_keeps_lives():
g = Game("cat")
g.guess("c")
assert g.lives == MAX_LIVES
def test_lost_when_out_of_lives():
g = Game("hi", lives=2)
g.guess("x")
assert not g.lost
g.guess("y")
assert g.lost and g.over and not g.won
+7 -1
View File
@@ -13,7 +13,7 @@ def test_play_until_won(monkeypatch, capsys):
out = run(monkeypatch, capsys, "hi", ["z", "h", "i"])
assert out.splitlines() == [
"_ _",
"Wrong, 'z' is not in the word.",
"Wrong, 'z' is not in the word. Lives left: 5",
"_ _",
"Right, 'h' is in the word.",
"h _",
@@ -26,3 +26,9 @@ def test_play_until_won(monkeypatch, capsys):
def test_repeated_letter_fills_all_blanks(monkeypatch, capsys):
out = run(monkeypatch, capsys, "aa", ["a"])
assert out.splitlines()[-2:] == ["a a", "You win!"]
def test_play_until_lost(monkeypatch, capsys):
out = run(monkeypatch, capsys, "a", list("bcdefg"))
assert "Lives left: 0" in out
assert out.splitlines()[-1] == "You lose. The word was: a"