…
…
Mask the secret word, track letter guesses with a set, count the lives left, and finish by writing a small bot that plays by letter frequency.
In hangman the player guesses the letters of a secret word one by one; each wrong guess costs a life. In this project you will build the logic of the game: masking the word, keeping the guesses in a set, catching repeated letters, and the winning and losing conditions. In the last step the computer plays by itself, starting with the letters that are most common in English.
Lessons used: loops and nested loops (10–13), string methods and formatting (21–22), lists (23–24), sets (29), comprehensions (30), sorting with key (31). You do not need to write functions.
The player's letters are in a ready-made list. Every cell sets the game up from scratch, so running it again does not change the result.
What you'll do
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
The secret word is programming. The screen must show underscores instead of letters, with one space between them: _ _ _ _ _ _ _ _ _ _ _.
" ".join(...) joins a sequence of strings with a space between them. Build a list with one "_" for each letter of the word using a list comprehension, and pass it to join.
This step's code cell is for members.Sign up free
You will keep the guessed letters in a set: a letter cannot appear twice in a set, and checking letter in guesses is very fast.
In the mask, guessed letters must show as themselves and the others as underscores. At the start of the comprehension, write the one-line conditional expression from lesson 8 (the tip in lesson 30): letter if letter in guesses else "_".
This step's code cell is for members.Sign up free
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
The player has 6 lives, and the letters are in a list, in order. For each letter:
1. If it was tried before, print Already tried: a and go to the next one (no life is lost).
2. Add the letter to the set.
3. If the word has it, print the new mask; if not, print the lives left.
4. If every letter of the word has been guessed, the player wins; if the lives run out, the player loses. In both cases, leave the loop.
You ask "have all the letters been guessed?" with sets: set(word) <= guesses means the word's set of letters is a subset of the guesses (the tip in lesson 29: a <= b asks whether all the elements of a are in b).
This step's code cell is for members.Sign up free
When the game ends, show the wrong guesses in alphabetical order. Set difference does this in one line: guesses - set(word) is the letters that were guessed but are not in the word. sorted turns a set into a sorted list.
Find the right guesses too, with set intersection (&).
This step's code cell is for members.Sign up free
Now let the computer play. In English, the most common letters come roughly in the order etaoinshrdlcumwfgypbvkjxqz. The bot says letters in this order; for each word, print how many letters it tried and how many were wrong.
The outer loop goes over the words, and the inner loop goes over the letters. The inner loop ends with break when the word is solved.
This step's code cell is for members.Sign up free
The number of distinct letters roughly decides how hard a word is in hangman: programming has 11 letters but only 8 distinct ones. Use a dictionary comprehension to build a dictionary with each word's number of distinct letters, then print the words from hardest to easiest by that number. The sorting line is ready: key=distinct.get sorts by the dictionary's value, as in lesson 31.
This step's code cell is for members.Sign up free