Guess the secret five-letter word in six tries. After each guess the game marks every letter: + means right letter in the right place, ? means the letter is in the word somewhere else, and - means it isn't in the word at all.
Writing the feedback is a lovely little string puzzle, especially when a word has the same letter twice. The challenges walk you through it.
Play Word Guess
Press βΆ Play to start the game. When it asks you something, type your answer and press Enter.
View and edit the code (38 lines)
How to play
- Type any five-letter word and press Enter.
- + right place, ? wrong place, - not in the word.
- Find the word in six guesses or fewer.
The complete Word Guess code
Here is the whole program (38 lines). It only uses Python's standard library, so it runs anywhere Python 3 is installed.
import random
WORDS = ["apple", "bread", "chair", "dance", "eagle", "flame", "grape", "house",
"juice", "lemon", "mango", "night", "ocean", "piano", "queen", "river",
"smile", "tiger", "under", "voice", "water", "youth", "zebra", "cloud",
"plant", "storm", "train", "brick", "sheep", "candy"]
def feedback(guess, secret):
marks = ["-"] * 5
left = {}
for g, s in zip(guess, secret): # count letters not matched exactly
if g != s:
left[s] = left.get(s, 0) + 1
for i, (g, s) in enumerate(zip(guess, secret)):
if g == s:
marks[i] = "+"
elif left.get(g, 0) > 0:
marks[i] = "?"
left[g] -= 1
return "".join(marks)
secret = random.choice(WORDS)
print("WORD GUESS - find the 5-letter word in 6 tries")
print("+ right place ? wrong place - not in the word")
for attempt in range(1, 7):
guess = input(f"\nGuess {attempt}/6: ").strip().lower()
while len(guess) != 5 or not guess.isalpha():
guess = input("Please type a 5-letter word: ").strip().lower()
marks = feedback(guess, secret)
print(" " + " ".join(guess.upper()))
print(" " + " ".join(marks))
if marks == "+++++":
print(f"\nYou got it in {attempt}!")
break
else:
print(f"\nOut of tries. The word was {secret.upper()}.")How the Word Guess game works, step by step
01Compare letters side by side
zip(guess, secret) pairs up the letters in the same positions, so g == s means a letter in exactly the right place.
for g, s in zip(guess, secret):
if g == s:
...02Count what's left over
Letters of the secret that weren't exact matches go into a counting dictionary. left.get(s, 0) + 1 adds one, starting from 0 for a new letter.
left[s] = left.get(s, 0) + 103Hand out ? marks fairly
A ? is only given while that letter still has spares in left, and each one uses a spare up. That stops a guess like EERIE getting three ? marks for a secret with one E.
elif left.get(g, 0) > 0:
marks[i] = "?"
left[g] -= 104Six tries with for ... else
The for loop counts attempts. break leaves early on a win; the else part runs only when all six tries are used.
for attempt in range(1, 7):
...
else:
print("Out of tries.")What you'll learn
zip()andenumerate()- Counting with a dictionary and
.get() - Building strings from lists with
.join() for ... else- Careful logic for repeated letters
Python concepts in this game:
Word Guess coding challenges
Write the key parts of the game yourself. Press Run to try your code and Check to test it. Each challenge you pass earns a star (saved in this browser).
Exact matches
Write exact(guess, secret) that returns how many positions have the same letter in both words.
Hint
zip(guess, secret) gives pairs of letters at the same position.
Show a solution
def exact(guess, secret):
count = 0
for g, s in zip(guess, secret):
if g == s:
count += 1
return count
print(exact("crane", "brave"))Count letters
Write letter_counts(word) that returns a dictionary of how many times each letter appears. letter_counts("sheep") is {"s": 1, "h": 1, "e": 2, "p": 1}.
Hint
counts[ch] = counts.get(ch, 0) + 1 inside a loop over the word.
Show a solution
def letter_counts(word):
counts = {}
for ch in word:
counts[ch] = counts.get(ch, 0) + 1
return counts
print(letter_counts("sheep"))The full feedback
Write feedback(guess, secret) returning 5 marks: + right place, ? in the word elsewhere, - otherwise. A letter only gets as many +/? marks as it appears in the secret.
Hint
First pass: count the secret letters that aren't exact matches. Second pass: + for exact; ? only if that letter still has a count above 0 (then subtract one).
Show a solution
def feedback(guess, secret):
marks = ["-"] * 5
left = {}
for g, s in zip(guess, secret):
if g != s:
left[s] = left.get(s, 0) + 1
for i in range(5):
if guess[i] == secret[i]:
marks[i] = "+"
elif left.get(guess[i], 0) > 0:
marks[i] = "?"
left[guess[i]] -= 1
return "".join(marks)
print(feedback("eerie", "there"))Make it your own
Once your challenges pass, try one of these upgrades:
- Only accept guesses that are real words from a word list.
- Add a hard mode: you must reuse letters you've found.
- Show a keyboard of used letters.
- Write a solver that guesses for you using the feedback.
Frequently asked questions
How do you make a word guessing game in Python?
Choose a secret word with random.choice, read guesses with input(), and for each guess compare letters position by position to build feedback until the word is found or the tries run out.
How do I handle repeated letters in the feedback?
Count the secret's letters that weren't exact matches, and only give a 'wrong place' mark while that letter still has a count left, using one up each time.
Where can I get a bigger word list?
Any text file with one word per line works. Read it with open(), keep the five-letter words, and pick from that list.