Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

selectWord promptUser displayWord checkWord hangman main function - initialize v

ID: 3644472 • Letter: S

Question

selectWord
promptUser
displayWord
checkWord
hangman main function - initialize variables
hangman main function - game while loop
hangman main function - check for game over (win or lose)
hangman main function - report game outcome
Project 7 code to get started with
For this project, you will start from a new editor window in IDLE. Save it as project7.py as soon as you have entered code that you would not like to lose.
Hangman

Hangman is a guessing game for two players. One player picks a secret word and writes out a series of dashes for each letter. The other player tries to discover this word by guessing letters. Every time they guess a letter from the secret word, the first player fills in that letter. The guesser wins if they can fill in all the letters in the secret word without making too many mistakes.

In this project, you will be implementing a game of hangman. The computer will select a word, and the user will have to try and guess it. This is a medium-size problem, but we can tackle it by breaking it into smaller pieces. Here is one way we could break down the game of hangman:

The computer picks a word
While the game hasn't ended:
prompt the user for a letter and store that letter for later
determine whether the guess is a good or bad one and take appropriate action
display the letters in their word that the user has guessed (leaving dashes for unknown letters)
determine whether the game is over
When the game is done, report the result of the game.

First, you can write helper functions to perform some of the smaller tasks. Afterwards, you can call your helper functions from a main hangman function.
Picking a word

First, the computer will need to select its word. Since it doesn't know any words on its own, you will need to provide a list of words for it to choose from. Come up with a list of words that you think would make for a good game of hangman. To make your game interesting, include at least 10 words in your list. Feel free to make your list as interesting as you want.

Once you have your list, you'll need to randomly pick a word from it. You can once again make use of Python's random module, which is full of useful methods for making random decisions. In particular, the method random.choice takes a list and returns a random element from the list.

Using the information above, write a function selectWord() that takes no arguments and returns a random word from your list of hangman words. If your list of random words contained 'cat', 'dog', and 'mouse', your selectWord function might work as follows:
>>>selectWord()
'cat'
>>>selectWord()
'mouse'
Guessing letters

Over the course of the game, your program will need to keep track of the letters the user guesses. A natural way to do this would be to create a list of letters (one-character strings). In the main hangman function, you can initialize a list-tracking variable to be the empty list, and then add in new letters as they are guessed.

Write a function promptUser(guessedLetters) that takes as argument a list of the letters that have been guessed so far. This function should prompt the user to guess a new letter, and then, if the letter hasn't already been guessed, return the new letter. (It's been a while since we've dealt with user input. If you want a refresher, you can look back to assignment 2 for examples of how to prompt a user for input.)

When complete, your promptUser function should work as follows (letterList is managed in the Python Shell in this example - later this will happen in the hangman main function):
>>> letterList = []
>>> letterList
[]
>>> next = promptUser(letterList)
Please select a letter: b
'b'
>>> letterList.append(next)
>>> letterList
['b']
>>> next = promptUser(letterList)
Please select a letter: b
"You've already guessed b. Try again."

Please select a letter: z
'z'
>>> letterList.append(next)
>>> letterList
['b', 'z']

EXTRA - As described, this function prompts a user for a letter, but the user is free to type whatever they like as input. For an added challenge, have this function check the user's input to make sure they select a valid letter. Invalid inputs might be numbers or strings containing more than 1 character.
Displaying words

The computer will need to incrementally display their word as the user guesses letters. Write a function displayWord(word, letters) that takes two arguments: a word and a list of letters. This function should print out all the letters of the word which have been guessed, leaving dashes for letters that have not been guessed yet.

When complete, your displayWord function should work as follows:
>>> displayWord("happy", ['h', 'p'])
h - p p -
>>> displayWord("bookkeeper", ['b', 'e', 'o'])
b o o - - e e - e -
>>> displayWord("rhythm", ['a', 'e', 'i', 'o', 'u'])
- - - - - -
>>> displayWord("happy", ['h', 'p', 'a', 'y'])
h a p p y
Checking words

The computer will need to incrementally display their word as the user guesses letters. Write a function checkWord(word, letters) that takes two arguments: a word and a list of letters. This function should return True if all of the characters of word appear in the letters list, and False otherwise.

When complete, your checkWord function should work as follows:
>>> checkWord("happy", ['h', 'p'])
False
>>> checkWord("bookkeeper", ['b', 'e', 'o'])
False
>>> checkWord("rhythm", ['a', 'e', 'i', 'o', 'u'])
False
>>> checkWord("happy", ['h', 'p', 'a', 'y'])
True
Putting it all together

Now you have many of the pieces you need to complete your hangman game. Write a function hangman() that plays a game of hangman with the user using the problem breakdown from earlier. Your main function will need to

Have the computer pick a word
Initialize variables needed to track the progress of the game, for example:
guessed letters
maximum number of bad guesses
the number of bad guesses so far
whether the word has been completely guessed
While the game hasn't ended - that is, the user has not reached the maximum number of bad guesses and the user hasn't guessed the word - your program should:
prompt the user for their next guess
determine whether the guess is a good or bad one and take appropriate action
display the word-so-far, with guessed letters appearing and dashes for unknown letters
determine whether the game is over.
When the game has ended, either because the user has reached the maximum number of bad guesses or because the user has guessed the word, report that information.

A complete game of hangman might look like this:
>>>hangman()
I've chosen my word
- - - - -

Guess a letter:
m

Good guess!
m - - - -

Guess a letter:
p

Oops, no. You have 4 more wrong guesses.
m - - - -

etc.

Explanation / Answer

Solution:

The Solution to your poblem is at given link:

www.cs.uoregon.edu/Classes/12S/cis122/project7.pdf