37 — Word guess: a hidden-word game
A hangman game hides a word and shows one blank per letter. A right guess
fills the blanks, a wrong one costs a try. The word comes from random_pick
(guide 10), and the loop is the menu loop from guide
26 aimed at letters instead of directions.
Steps
Keep a list of secret words and pick one at random with
random_pick:
run it →use random latest words = ["apple", "grape", "melon", "mango", "orange"] secret = random_pick(words) show secretRun it a few times: a different word appears each time.
Show the word as one blank per letter. A list of
"_"strings starts hidden, and" ".join(shown)prints them with spaces:
run it →secret = "grape" shown = [] for ch in secret: shown.append("_") show " ".join(shown)_ _ _ _ _means five letters.Reveal a guessed letter by walking the word with a
forloop and writing the letter into the matching blank.for i in range(len(secret))visits every position, andshown[i] = letterfills the blank when it matches:
run it →secret = "grape" shown = [] for ch in secret: shown.append("_") ask letter, "guess a letter: " for i in range(len(secret)): if secret[i] == letter: shown[i] = letter show " ".join(shown)Guessing
aturns_ _ _ _ _into_ _ a _ _.The full game adds a try counter and win/lose checks.
while remaining > 0keeps asking, a wrong guess lowersremaining,"_" not in shownmeans every letter was found, andbreakleaves the loop. Save it asword-guess.nme:
run it →# A hidden-word game: guess the letters of a secret word. # Run: nme r word-guess use random latest words = ["apple", "grape", "melon", "mango", "orange"] secret = random_pick(words) shown = [] for ch in secret: shown.append("_") remaining = 6 guessed = [] show "I picked a word with " + str(len(secret)) + " letters." while remaining > 0: show " ".join(shown) show "tries left: " + str(remaining) ask letter, "guess a letter (q to quit): " if letter == "q": show "the word was " + secret break if letter in guessed: show "you already guessed " + letter continue guessed.append(letter) if letter in secret: for i in range(len(secret)): if secret[i] == letter: shown[i] = letter show "yes, " + letter + " is in the word" if "_" not in shown: show " ".join(shown) show "you won! the word was " + secret break else: remaining = remaining - 1 show "no, " + letter + " is not in the word" if remaining == 0: show "you lost. the word was " + secretRun it and guess four letters, then quit:
printf 'e\na\nr\nq\n' | nme r word-guessI picked a word with 5 letters. _ _ _ _ _ tries left: 6 guess a letter (q to quit): yes, e is in the word _ _ _ _ e tries left: 6 guess a letter (q to quit): yes, a is in the word _ _ a _ e tries left: 6 guess a letter (q to quit): yes, r is in the word _ r a _ e tries left: 6 guess a letter (q to quit): the word was grapeThe hidden word is picked at random, so your run shows a different word and a different blank pattern.
Try it yourself
Make the game accept capital letters. Right after ask letter, ... add
letter = letter.lower() so typing A guesses the same as a:
secret = "grape"
shown = []
for ch in secret:
shown.append("_")
ask letter, "guess a letter: "
letter = letter.lower()
for i in range(len(secret)):
if secret[i] == letter:
shown[i] = letter
show " ".join(shown)
run it →Then add the same line to the full game and test it with A and G.
What you learned
random_pick(words)picks the hidden word from a list.- A list of
"_"shows one blank per letter;" ".join(shown)prints it. - A
forloop overrange(len(secret))reveals each matching letter. while remaining > 0counts down the tries andbreakleaves the loop.