Step 1
TirSystem/github-action: Sync GitHub mirror metadata / sync-metadata (push) Successful in 6s

Picking a random word and checking answers
This commit is contained in:
2026-10-04 12:31:58 +08:00
commit f07c34a5fa
12 changed files with 530 additions and 0 deletions
+4
View File
@@ -0,0 +1,4 @@
"""!
@file __init__.py
@brief Hangman, a beginner-friendly terminal game.
"""
+8
View File
@@ -0,0 +1,8 @@
"""!
@file __main__.py
@brief Allows running the package with `python -m hangman`.
"""
from hangman.main import main
main()
+23
View File
@@ -0,0 +1,23 @@
"""!
@file constants.py
@brief Constants shared across the Hangman game.
"""
## Words the computer picks from.
WORD_LIST = [
"aardvark",
"baboon",
"camel",
"dolphin",
"elephant",
"flamingo",
"giraffe",
"hedgehog",
"iguana",
"kangaroo",
"leopard",
"octopus",
"penguin",
"squirrel",
"tortoise",
]
+27
View File
@@ -0,0 +1,27 @@
"""!
@file game.py
@brief Game logic for Hangman, kept free of input/output so it can be tested.
"""
import random
from hangman.constants import WORD_LIST
def choose_word(words: list[str] | None = None) -> str:
"""!
@brief Pick a random word.
@param words Candidate words; defaults to the built-in word list.
@return The chosen word in lower case.
"""
return random.choice(words if words is not None else WORD_LIST).lower()
def is_in_word(word: str, letter: str) -> bool:
"""!
@brief Check whether a guessed letter occurs in the word.
@param word The secret word.
@param letter The guessed letter (case-insensitive).
@return True if @p letter is in @p word.
"""
return letter.lower() in word.lower()
+22
View File
@@ -0,0 +1,22 @@
"""!
@file main.py
@brief Command-line interface for Hangman.
"""
from hangman.game import choose_word, is_in_word
def main() -> None:
"""!
@brief Entry point: pick a random word and check one guessed letter.
"""
word = choose_word()
guess = input("Guess a letter: ").strip()
if is_in_word(word, guess):
print(f"Right, '{guess.lower()}' is in the word.")
else:
print(f"Wrong, '{guess.lower()}' is not in the word.")
if __name__ == "__main__":
main()