MIT 6.100L Introduction to CS and Programming Using Python, Fall 2022 · Problem set 2, 2) The Game, with 2.4) The Game with Help — hangman · Dr. Ana Bell · CC BY-NC-SA 4.0 · MIT publishes no solution for this set; the worked solution is ours
Implement the function hangman(secret_word, with_help), which plays an interactive game of Hangman between the user and the computer: the computer has picked a secret word, and the player tries to guess its letters.
This is part 2 of MIT's Problem Set 2, Hangman. The three helper functions from part 1 — has_player_won, get_word_progress and get_available_letters, the ones you wrote in the earlier Hangman challenges — are given, complete, at the top of the starter. In designing your code, be sure you take advantage of them! You may write additional helper functions if you need them.
hangman takes two parameters: (1) secret_word, the secret word the user is to guess; and (2) with_help, a boolean representing whether or not the game is to be played with the 'help' functionality.
Important: Do NOT change the name, input parameters, or specifications of the given functions or of hangman.
The general behavior:
There is also a feature that makes the game easier: when the game is played with help, the user can input a special 'help' character, !, that reveals an unguessed letter at the expense of losing more guesses.
secret_word along with the boolean with_help are passed into the hangman function as parameters.secret_word contains.Welcome to Hangman! I am thinking of a word that is 4 letters long.
--------------) to separate individual guesses from each other. Leaving out the row of dashes will cause the tests to fail.input("Please guess a letter: ").
!).*)Example Game Implementation 1 (on each Please guess a letter: line, what follows the colon is the user's input):
Welcome to Hangman! I am thinking of a word that is 4 letters long. -------------- You have 10 guesses left. Available letters: abcdefghijklmnopqrstuvwxyz Please guess a letter: a Good guess: *a** -------------- You have 10 guesses left. Available letters: bcdefghijklmnopqrstuvwxyz Please guess a letter: b Oops! That letter is not in my word: *a** -------------- You have 9 guesses left. Available letters: cdefghijklmnopqrstuvwxyz Please guess a letter: 2 Oops! That is not a valid letter. Please input a letter from the alphabet: *a** -------------- You have 9 guesses left. Available letters: cdefghijklmnopqrstuvwxyz Please guess a letter: foo Oops! That is not a valid letter. Please input a letter from the alphabet: *a** -------------- You have 9 guesses left. Available letters: cdefghijklmnopqrstuvwxyz Please guess a letter: + Oops! That is not a valid letter. Please input a letter from the alphabet: *a**
Check that the user input is an alphabet letter (or the help character if the game is played with help); if it is not, tell the user they can only input a letter from the alphabet. The string methods str.isalpha() and str.lower() may help:
my_string = "HeLLoWoRlD" print(my_string.isalpha()) # True print(my_string.lower()) # helloworld
If the user inputs:
! is also a valid input.a, e, i, o, and u. The letter y does not count as a vowel. Note: if a user inputs an incorrect vowel that hasn't been guessed and there is only one guess remaining, the user loses and the game is over.Example Game Implementation 1 (continued):
You have 9 guesses left. Available letters: bcdefghijklmnopqrtuvwxyz Please guess a letter: t Good guess: ta*t -------------- You have 9 guesses left. Available letters: bcdefghijklmnopqruvwxyz Please guess a letter: e Oops! That letter is not in my word: ta*t -------------- You have 7 guesses left. Available letters: bcdfghijklmnopqruvwxyz Please guess a letter: e Oops! You've already guessed that letter: ta*t
It isn't always easy to beat the computer, especially when it selects an esoteric word. It might be nice if you could ask for some help. To do this you will create a feature of the game that works as follows:
!, the computer will provide you with one of the missing letters in the secret word at a cost of three guesses. This character should be the only non-letter input that your game accepts as a guess.Note: the user can play the game with this feature only when the with_help parameter is True.
As a starting point, we suggest writing a helper function that chooses a letter to reveal. It should take two arguments: the secret word and the string of available letters (from get_available_letters). This helper function should create a string choose_from containing the unique letters that are in both the secret word and the available letters. You can then use the following statements to pick a random character revealed_letter from that string:
new = random.randint(0, len(choose_from)-1) revealed_letter = choose_from[new]
Your helper function should then return this revealed_letter. Back in your original game logic, you'll need to add a conditional statement to catch the case of the user inputting !. This case, if triggered, can add the letter returned by your helper function to letters_guessed, show the new word progress, decrement the remaining guesses by 3, and continue the gameplay.
Example Game Implementation 2:
Welcome to Hangman! I am thinking of a word that is 7 letters long. -------------- You currently have 10 guesses left. Available letters: abcdefghijklmnopqrstuvwxyz Please guess a letter: ! Letter revealed: r r*****r -------------- You currently have 7 guesses left. Available letters: abcdefghijklmnopqstuvwxyz Please guess a letter: ! Letter revealed: a ra***ar -------------- You currently have 4 guesses left. Available letters: bcdefghijklmnopqstuvwxyz Please guess a letter: ! Letter revealed: e ra*e*ar -------------- You currently have 1 guess left. Available letters: bcdfghijklmnopqstuvwxyz Please guess a letter: ! Oops! Not enough guesses left: ra*e*ar
secret_word or has 0 guesses remaining.total_score = (guesses_remaining + 4 * number of unique letters in secret_word) + (3 * length of secret_word)a, s, l, e, and p). Thus, the final score is: (6 + 4 * 5) + (3 * 6) = 44.Example Implementation (win):
# ... snip ... You have 5 guesses left. Available letters: abcgnqrstuvwxyz Please guess a letter: n Good guess: dolphin -------------- Congratulations, you won! Your total score for this game is: 54
Example Implementation (lose):
# ... snip ... You have 1 guess left. Available Letters: ghijklmnopqrstuvwxyz Please guess a letter: i Oops! That letter is not in my word: e**e -------------- Sorry, you ran out of guesses. The word was else.
# ... snip ... is not part of the output; it indicates that only part of the game is shown.
A winning game:
Welcome to Hangman! I am thinking of a word that is 4 letters long. -------------- You have 10 guesses left. Available letters: abcdefghijklmnopqrstuvwxyz Please guess a letter: a Good guess: *a** -------------- You have 10 guesses left. Available letters: bcdefghijklmnopqrstuvwxyz Please guess a letter: a Oops! You've already guessed that letter: *a** -------------- You have 10 guesses left. Available letters: bcdefghijklmnopqrstuvwxyz Please guess a letter: s Oops! That letter is not in my word: *a** -------------- You have 9 guesses left. Available letters: bcdefghijklmnopqrtuvwxyz Please guess a letter: + Oops! That is not a valid letter. Please input a letter from the alphabet: *a** -------------- You have 9 guesses left. Available letters: bcdefghijklmnopqrtuvwxyz Please guess a letter: t Good guess: ta*t -------------- You have 9 guesses left. Available letters: bcdefghijklmnopqruvwxyz Please guess a letter: e Oops! That letter is not in my word: ta*t -------------- You have 7 guesses left. Available letters: bcdfghijklmnopqruvwxyz Please guess a letter: c Good guess: tact -------------- Congratulations, you won! Your total score for this game is: 31
A game with help:
Welcome to Hangman! I am thinking of a word that is 7 letters long -------------- You currently have 10 guesses left Available letters: abcdefghijklmnopqrstuvwxyz Please guess a letter: r Good guess: r*****r -------------- You currently have 10 guesses left Available letters: abcdefghijklmnopqstuvwxyz Please guess a letter: ! Letter revealed: c r*c*c*r -------------- You currently have 7 guesses left Available letters: abdefghijklmnopqstuvwxyz Please guess a letter: ! Letter revealed: a rac*car -------------- You currently have 4 guesses left Available letters: bdefghijklmnopqstuvwxyz Please guess a letter: e Good guess: racecar -------------- Congratulations, you won! Your total score for this game is: 41
A few available-letters lines in MIT's handout have typos (a revealed or guessed letter still listed, or an unguessed one missing); they are corrected in these games.
In this editor nobody can type into input(), so the tests play the games for you. Don't call hangman yourself at the top level of your code. Each test calls hangman with a secret word and answers every input() call with the next guess from a script. It captures everything you print, splits it on each row of three or more dashes, and checks each turn for the essential content, as MIT's tester does:
10 guesses (or 1 guess)revealed after a ! that reveals a letterscore and the score when the player wins, or the secret word when they loseThe rest of the wording is up to you. A game that asks for another guess after it should have ended fails the test.
Adapted for the browser: MIT's hangman.py loads a 55,900-word list from words.txt and picks the secret word at random (load_words, choose_word). Here the tests pass the secret word in, so those helpers, the word list and the Loading word list from file... lines of MIT's example games are left out. Instead of a person typing, the tests feed input() scripted guesses. While they play a game with help, they swap random.randint for a predictable stand-in, so the game plays out the same way on every run; any missing letter your code reveals passes.